From 0959b74b3c2fedbdc3f902bc395166c881f06bf9 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 11:12:58 -0800 Subject: [PATCH 1/2] move misplaced tests --- .../tests/indexes/timedeltas/test_constructors.py | 6 ++++++ pandas/tests/indexes/timedeltas/test_timedelta.py | 14 -------------- pandas/tests/series/indexing/test_indexing.py | 10 ++++++++++ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/pandas/tests/indexes/timedeltas/test_constructors.py b/pandas/tests/indexes/timedeltas/test_constructors.py index 32e6821e87f05..0de10b5d82171 100644 --- a/pandas/tests/indexes/timedeltas/test_constructors.py +++ b/pandas/tests/indexes/timedeltas/test_constructors.py @@ -10,6 +10,12 @@ class TestTimedeltaIndex: + @pytest.mark.parametrize("unit", ["Y", "y", "M"]) + def test_unit_m_y_raises(self, unit): + msg = "Units 'M' and 'Y' are no longer supported" + with pytest.raises(ValueError, match=msg): + TimedeltaIndex([1, 3, 7], unit) + def test_int64_nocopy(self): # GH#23539 check that a copy isn't made when we pass int64 data # and copy=False diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 3b52b93fa6369..8a91c9d5e09c8 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -284,17 +284,3 @@ def test_freq_conversion(self): result = td.astype("timedelta64[s]") tm.assert_index_equal(result, expected) - - @pytest.mark.parametrize("unit", ["Y", "y", "M"]) - def test_unit_m_y_raises(self, unit): - msg = "Units 'M' and 'Y' are no longer supported" - with pytest.raises(ValueError, match=msg): - TimedeltaIndex([1, 3, 7], unit) - - -class TestTimeSeries: - def test_series_box_timedelta(self): - rng = timedelta_range("1 day 1 s", periods=5, freq="h") - s = Series(rng) - assert isinstance(s[1], Timedelta) - assert isinstance(s.iat[2], Timedelta) diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 1b21e2419595f..321df29176728 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -241,6 +241,16 @@ def test_series_box_timestamp(): assert isinstance(ser.iat[5], pd.Timestamp) +def test_series_box_timedelta(): + rng = pd.timedelta_range("1 day 1 s", periods=5, freq="h") + ser = pd.Series(rng) + assert isinstance(ser[0], Timedelta) + assert isinstance(ser.at[1], Timedelta) + assert isinstance(ser.iat[2], Timedelta) + assert isinstance(ser.loc[3], Timedelta) + assert isinstance(ser.iloc[4], Timedelta) + + def test_getitem_ambiguous_keyerror(): s = Series(range(10), index=list(range(0, 20, 2))) with pytest.raises(KeyError, match=r"^1$"): From 23ee2fc7f3cd996d946b4c61a810cf248b8b68f5 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 6 Feb 2020 11:22:18 -0800 Subject: [PATCH 2/2] move misplaced timedeltaIndex tests --- pandas/tests/frame/indexing/test_indexing.py | 10 +++++++++- pandas/tests/indexes/timedeltas/test_ops.py | 12 ------------ .../indexes/timedeltas/test_timedelta_range.py | 18 ------------------ pandas/tests/indexes/timedeltas/test_tools.py | 11 +++++++++++ 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index ca4d1ff067f3d..19b72d42062aa 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -94,6 +94,14 @@ def test_loc_iterable(self, float_frame, key_type): expected = float_frame.loc[:, ["A", "B", "C"]] tm.assert_frame_equal(result, expected) + def test_loc_timedelta_0seconds(self): + # GH#10583 + df = pd.DataFrame(np.random.normal(size=(10, 4))) + df.index = pd.timedelta_range(start="0s", periods=10, freq="s") + expected = df.loc[pd.Timedelta("0s") :, :] + result = df.loc["0s":, :] + tm.assert_frame_equal(expected, result) + @pytest.mark.parametrize( "idx_type", [ @@ -204,7 +212,7 @@ def test_setitem_list_of_tuples(self, float_frame): expected = Series(tuples, index=float_frame.index, name="tuples") tm.assert_series_equal(result, expected) - def test_setitem_mulit_index(self): + def test_setitem_multi_index(self): # GH7655, test that assigning to a sub-frame of a frame # with multi-index columns aligns both rows and columns it = ["jim", "joe", "jolie"], ["first", "last"], ["left", "center", "right"] diff --git a/pandas/tests/indexes/timedeltas/test_ops.py b/pandas/tests/indexes/timedeltas/test_ops.py index a3e390fc941c7..6606507dabc29 100644 --- a/pandas/tests/indexes/timedeltas/test_ops.py +++ b/pandas/tests/indexes/timedeltas/test_ops.py @@ -113,15 +113,6 @@ def test_order(self): ["1 day", "3 day", "5 day", "2 day", "1 day"], name="idx2" ) - # TODO(wesm): unused? - # exp2 = TimedeltaIndex(['1 day', '1 day', '2 day', - # '3 day', '5 day'], name='idx2') - - # idx3 = TimedeltaIndex([pd.NaT, '3 minute', '5 minute', - # '2 minute', pd.NaT], name='idx3') - # exp3 = TimedeltaIndex([pd.NaT, pd.NaT, '2 minute', '3 minute', - # '5 minute'], name='idx3') - for idx, expected in [(idx1, exp1), (idx1, exp1), (idx1, exp1)]: ordered = idx.sort_values() tm.assert_index_equal(ordered, expected) @@ -189,9 +180,6 @@ def test_infer_freq(self, freq): tm.assert_index_equal(idx, result) assert result.freq == freq - def test_shift(self): - pass # handled in test_arithmetic.py - def test_repeat(self): index = pd.timedelta_range("1 days", periods=2, freq="D") exp = pd.TimedeltaIndex(["1 days", "1 days", "2 days", "2 days"]) diff --git a/pandas/tests/indexes/timedeltas/test_timedelta_range.py b/pandas/tests/indexes/timedeltas/test_timedelta_range.py index 1cef9de6a3a77..9f12af9a96104 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta_range.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta_range.py @@ -1,7 +1,6 @@ import numpy as np import pytest -import pandas as pd from pandas import timedelta_range, to_timedelta import pandas._testing as tm @@ -31,23 +30,6 @@ def test_timedelta_range(self): result = timedelta_range("0 days", freq="30T", periods=50) tm.assert_index_equal(result, expected) - # GH 11776 - arr = np.arange(10).reshape(2, 5) - df = pd.DataFrame(np.arange(10).reshape(2, 5)) - for arg in (arr, df): - with pytest.raises(TypeError, match="1-d array"): - to_timedelta(arg) - for errors in ["ignore", "raise", "coerce"]: - with pytest.raises(TypeError, match="1-d array"): - to_timedelta(arg, errors=errors) - - # issue10583 - df = pd.DataFrame(np.random.normal(size=(10, 4))) - df.index = pd.timedelta_range(start="0s", periods=10, freq="s") - expected = df.loc[pd.Timedelta("0s") :, :] - result = df.loc["0s":, :] - tm.assert_frame_equal(expected, result) - @pytest.mark.parametrize( "periods, freq", [(3, "2D"), (5, "D"), (6, "19H12T"), (7, "16H"), (9, "12H")] ) diff --git a/pandas/tests/indexes/timedeltas/test_tools.py b/pandas/tests/indexes/timedeltas/test_tools.py index 477fc092a4e16..e3cf3a7f16a82 100644 --- a/pandas/tests/indexes/timedeltas/test_tools.py +++ b/pandas/tests/indexes/timedeltas/test_tools.py @@ -57,6 +57,17 @@ def test_to_timedelta(self): expected = TimedeltaIndex([np.timedelta64(1, "D")] * 5) tm.assert_index_equal(result, expected) + def test_to_timedelta_dataframe(self): + # GH 11776 + arr = np.arange(10).reshape(2, 5) + df = pd.DataFrame(np.arange(10).reshape(2, 5)) + for arg in (arr, df): + with pytest.raises(TypeError, match="1-d array"): + to_timedelta(arg) + for errors in ["ignore", "raise", "coerce"]: + with pytest.raises(TypeError, match="1-d array"): + to_timedelta(arg, errors=errors) + def test_to_timedelta_invalid(self): # bad value for errors parameter