From 852fd640335c9e17f1b9600ac36f7b8f9700d9e0 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 30 Jan 2021 14:31:24 -0800 Subject: [PATCH] TST/REF: misplaced indexing tests --- pandas/tests/frame/indexing/test_indexing.py | 28 --------------- pandas/tests/frame/methods/test_reindex.py | 31 +++++++++++++++-- pandas/tests/series/indexing/test_indexing.py | 34 ++----------------- pandas/tests/series/indexing/test_numeric.py | 25 ++++++-------- pandas/tests/series/indexing/test_setitem.py | 23 +++++++++++++ 5 files changed, 65 insertions(+), 76 deletions(-) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 3b1a8ebcb13d0..6808ffe65e561 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1732,34 +1732,6 @@ def test_setitem(self, uint64_frame): ) -@pytest.mark.parametrize( - "src_idx", - [ - Index([]), - pd.CategoricalIndex([]), - ], -) -@pytest.mark.parametrize( - "cat_idx", - [ - # No duplicates - Index([]), - pd.CategoricalIndex([]), - Index(["A", "B"]), - pd.CategoricalIndex(["A", "B"]), - # Duplicates: GH#38906 - Index(["A", "A"]), - pd.CategoricalIndex(["A", "A"]), - ], -) -def test_reindex_empty(src_idx, cat_idx): - df = DataFrame(columns=src_idx, index=["K"], dtype="f8") - - result = df.reindex(columns=cat_idx) - expected = DataFrame(index=["K"], columns=cat_idx, dtype="f8") - tm.assert_frame_equal(result, expected) - - def test_object_casting_indexing_wraps_datetimelike(): # GH#31649, check the indexing methods all the way down the stack df = DataFrame( diff --git a/pandas/tests/frame/methods/test_reindex.py b/pandas/tests/frame/methods/test_reindex.py index c49375758345c..9116b1ff5ad65 100644 --- a/pandas/tests/frame/methods/test_reindex.py +++ b/pandas/tests/frame/methods/test_reindex.py @@ -151,7 +151,7 @@ def test_reindex_methods_nearest_special(self): def test_reindex_nearest_tz(self, tz_aware_fixture): # GH26683 tz = tz_aware_fixture - idx = pd.date_range("2019-01-01", periods=5, tz=tz) + idx = date_range("2019-01-01", periods=5, tz=tz) df = DataFrame({"x": list(range(5))}, index=idx) expected = df.head(3) @@ -759,7 +759,7 @@ def test_reindex_multi(self): def test_reindex_multi_categorical_time(self): # https://github.com/pandas-dev/pandas/issues/21390 - midx = pd.MultiIndex.from_product( + midx = MultiIndex.from_product( [ Categorical(["a", "b", "c"]), Categorical(date_range("2012-01-01", periods=3, freq="H")), @@ -906,3 +906,30 @@ def test_reindex_empty_frame(self, kwargs): result = df.reindex(idx, **kwargs) expected = DataFrame({"a": [pd.NA] * 3}, index=idx) tm.assert_frame_equal(result, expected) + + @pytest.mark.parametrize( + "src_idx", + [ + Index([]), + CategoricalIndex([]), + ], + ) + @pytest.mark.parametrize( + "cat_idx", + [ + # No duplicates + Index([]), + CategoricalIndex([]), + Index(["A", "B"]), + CategoricalIndex(["A", "B"]), + # Duplicates: GH#38906 + Index(["A", "A"]), + CategoricalIndex(["A", "A"]), + ], + ) + def test_reindex_empty(self, src_idx, cat_idx): + df = DataFrame(columns=src_idx, index=["K"], dtype="f8") + + result = df.reindex(columns=cat_idx) + expected = DataFrame(index=["K"], columns=cat_idx, dtype="f8") + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index dbc751dd614a1..c9e505ef4bbaf 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -607,29 +607,6 @@ def test_td64_series_assign_nat(nat_val, should_cast): tm.assert_series_equal(ser, expected) -@pytest.mark.parametrize( - "td", - [ - Timedelta("9 days"), - Timedelta("9 days").to_timedelta64(), - Timedelta("9 days").to_pytimedelta(), - ], -) -def test_append_timedelta_does_not_cast(td): - # GH#22717 inserting a Timedelta should _not_ cast to int64 - expected = Series(["x", td], index=[0, "td"], dtype=object) - - ser = Series(["x"]) - ser["td"] = td - tm.assert_series_equal(ser, expected) - assert isinstance(ser["td"], Timedelta) - - ser = Series(["x"]) - ser.loc["td"] = Timedelta("9 days") - tm.assert_series_equal(ser, expected) - assert isinstance(ser["td"], Timedelta) - - def test_underlying_data_conversion(): # GH 4080 df = DataFrame({c: [1, 2, 3] for c in ["a", "b", "c"]}) @@ -759,15 +736,11 @@ def test_getitem_unrecognized_scalar(): timedelta_range("0", periods=20, freq="H"), ], ) -def test_slice_with_zero_step_raises(index): - ts = Series(np.arange(20), index) +def test_slice_with_zero_step_raises(index, frame_or_series, indexer_sli): + ts = frame_or_series(np.arange(20), index=index) with pytest.raises(ValueError, match="slice step cannot be zero"): - ts[::0] - with pytest.raises(ValueError, match="slice step cannot be zero"): - ts.loc[::0] - with pytest.raises(ValueError, match="slice step cannot be zero"): - ts.iloc[::0] + indexer_sli(ts)[::0] @pytest.mark.parametrize( @@ -784,7 +757,6 @@ def assert_slices_equivalent(l_slc, i_slc): tm.assert_series_equal(ts[l_slc], expected) tm.assert_series_equal(ts.loc[l_slc], expected) - tm.assert_series_equal(ts.loc[l_slc], expected) keystr1 = str(index[9]) keystr2 = str(index[13]) diff --git a/pandas/tests/series/indexing/test_numeric.py b/pandas/tests/series/indexing/test_numeric.py index 4caf6d03d8d80..10b9360802c1c 100644 --- a/pandas/tests/series/indexing/test_numeric.py +++ b/pandas/tests/series/indexing/test_numeric.py @@ -4,29 +4,24 @@ import pandas._testing as tm -def test_slice_float64(): +def test_slice_float64(frame_or_series): values = np.arange(10.0, 50.0, 2) index = Index(values) start, end = values[[5, 15]] - s = Series(np.random.randn(20), index=index) + data = np.random.randn(20, 3) + if frame_or_series is not DataFrame: + data = data[:, 0] - result = s[start:end] - expected = s.iloc[5:16] - tm.assert_series_equal(result, expected) - - result = s.loc[start:end] - tm.assert_series_equal(result, expected) - - df = DataFrame(np.random.randn(20, 3), index=index) + obj = frame_or_series(data, index=index) - result = df[start:end] - expected = df.iloc[5:16] - tm.assert_frame_equal(result, expected) + result = obj[start:end] + expected = obj.iloc[5:16] + tm.assert_equal(result, expected) - result = df.loc[start:end] - tm.assert_frame_equal(result, expected) + result = obj.loc[start:end] + tm.assert_equal(result, expected) def test_getitem_setitem_slice_bug(): diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 9ace404930876..6f076851266f3 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -9,6 +9,7 @@ MultiIndex, NaT, Series, + Timedelta, Timestamp, date_range, period_range, @@ -470,6 +471,28 @@ def test_setitem_empty_series_timestamp_preserves_dtype(self): result = series["timestamp"] assert result == expected + @pytest.mark.parametrize( + "td", + [ + Timedelta("9 days"), + Timedelta("9 days").to_timedelta64(), + Timedelta("9 days").to_pytimedelta(), + ], + ) + def test_append_timedelta_does_not_cast(self, td): + # GH#22717 inserting a Timedelta should _not_ cast to int64 + expected = Series(["x", td], index=[0, "td"], dtype=object) + + ser = Series(["x"]) + ser["td"] = td + tm.assert_series_equal(ser, expected) + assert isinstance(ser["td"], Timedelta) + + ser = Series(["x"]) + ser.loc["td"] = Timedelta("9 days") + tm.assert_series_equal(ser, expected) + assert isinstance(ser["td"], Timedelta) + def test_setitem_scalar_into_readonly_backing_data(): # GH#14359: test that you cannot mutate a read only buffer