Skip to content

Commit 85eeef3

Browse files
authored
TST/REF: misplaced indexing tests (#39598)
1 parent ca4f204 commit 85eeef3

File tree

5 files changed

+65
-76
lines changed

5 files changed

+65
-76
lines changed

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,34 +1732,6 @@ def test_setitem(self, uint64_frame):
17321732
)
17331733

17341734

1735-
@pytest.mark.parametrize(
1736-
"src_idx",
1737-
[
1738-
Index([]),
1739-
pd.CategoricalIndex([]),
1740-
],
1741-
)
1742-
@pytest.mark.parametrize(
1743-
"cat_idx",
1744-
[
1745-
# No duplicates
1746-
Index([]),
1747-
pd.CategoricalIndex([]),
1748-
Index(["A", "B"]),
1749-
pd.CategoricalIndex(["A", "B"]),
1750-
# Duplicates: GH#38906
1751-
Index(["A", "A"]),
1752-
pd.CategoricalIndex(["A", "A"]),
1753-
],
1754-
)
1755-
def test_reindex_empty(src_idx, cat_idx):
1756-
df = DataFrame(columns=src_idx, index=["K"], dtype="f8")
1757-
1758-
result = df.reindex(columns=cat_idx)
1759-
expected = DataFrame(index=["K"], columns=cat_idx, dtype="f8")
1760-
tm.assert_frame_equal(result, expected)
1761-
1762-
17631735
def test_object_casting_indexing_wraps_datetimelike():
17641736
# GH#31649, check the indexing methods all the way down the stack
17651737
df = DataFrame(

pandas/tests/frame/methods/test_reindex.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def test_reindex_methods_nearest_special(self):
151151
def test_reindex_nearest_tz(self, tz_aware_fixture):
152152
# GH26683
153153
tz = tz_aware_fixture
154-
idx = pd.date_range("2019-01-01", periods=5, tz=tz)
154+
idx = date_range("2019-01-01", periods=5, tz=tz)
155155
df = DataFrame({"x": list(range(5))}, index=idx)
156156

157157
expected = df.head(3)
@@ -759,7 +759,7 @@ def test_reindex_multi(self):
759759

760760
def test_reindex_multi_categorical_time(self):
761761
# https://github.com/pandas-dev/pandas/issues/21390
762-
midx = pd.MultiIndex.from_product(
762+
midx = MultiIndex.from_product(
763763
[
764764
Categorical(["a", "b", "c"]),
765765
Categorical(date_range("2012-01-01", periods=3, freq="H")),
@@ -906,3 +906,30 @@ def test_reindex_empty_frame(self, kwargs):
906906
result = df.reindex(idx, **kwargs)
907907
expected = DataFrame({"a": [pd.NA] * 3}, index=idx)
908908
tm.assert_frame_equal(result, expected)
909+
910+
@pytest.mark.parametrize(
911+
"src_idx",
912+
[
913+
Index([]),
914+
CategoricalIndex([]),
915+
],
916+
)
917+
@pytest.mark.parametrize(
918+
"cat_idx",
919+
[
920+
# No duplicates
921+
Index([]),
922+
CategoricalIndex([]),
923+
Index(["A", "B"]),
924+
CategoricalIndex(["A", "B"]),
925+
# Duplicates: GH#38906
926+
Index(["A", "A"]),
927+
CategoricalIndex(["A", "A"]),
928+
],
929+
)
930+
def test_reindex_empty(self, src_idx, cat_idx):
931+
df = DataFrame(columns=src_idx, index=["K"], dtype="f8")
932+
933+
result = df.reindex(columns=cat_idx)
934+
expected = DataFrame(index=["K"], columns=cat_idx, dtype="f8")
935+
tm.assert_frame_equal(result, expected)

pandas/tests/series/indexing/test_indexing.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -607,29 +607,6 @@ def test_td64_series_assign_nat(nat_val, should_cast):
607607
tm.assert_series_equal(ser, expected)
608608

609609

610-
@pytest.mark.parametrize(
611-
"td",
612-
[
613-
Timedelta("9 days"),
614-
Timedelta("9 days").to_timedelta64(),
615-
Timedelta("9 days").to_pytimedelta(),
616-
],
617-
)
618-
def test_append_timedelta_does_not_cast(td):
619-
# GH#22717 inserting a Timedelta should _not_ cast to int64
620-
expected = Series(["x", td], index=[0, "td"], dtype=object)
621-
622-
ser = Series(["x"])
623-
ser["td"] = td
624-
tm.assert_series_equal(ser, expected)
625-
assert isinstance(ser["td"], Timedelta)
626-
627-
ser = Series(["x"])
628-
ser.loc["td"] = Timedelta("9 days")
629-
tm.assert_series_equal(ser, expected)
630-
assert isinstance(ser["td"], Timedelta)
631-
632-
633610
def test_underlying_data_conversion():
634611
# GH 4080
635612
df = DataFrame({c: [1, 2, 3] for c in ["a", "b", "c"]})
@@ -759,15 +736,11 @@ def test_getitem_unrecognized_scalar():
759736
timedelta_range("0", periods=20, freq="H"),
760737
],
761738
)
762-
def test_slice_with_zero_step_raises(index):
763-
ts = Series(np.arange(20), index)
739+
def test_slice_with_zero_step_raises(index, frame_or_series, indexer_sli):
740+
ts = frame_or_series(np.arange(20), index=index)
764741

765742
with pytest.raises(ValueError, match="slice step cannot be zero"):
766-
ts[::0]
767-
with pytest.raises(ValueError, match="slice step cannot be zero"):
768-
ts.loc[::0]
769-
with pytest.raises(ValueError, match="slice step cannot be zero"):
770-
ts.iloc[::0]
743+
indexer_sli(ts)[::0]
771744

772745

773746
@pytest.mark.parametrize(
@@ -784,7 +757,6 @@ def assert_slices_equivalent(l_slc, i_slc):
784757

785758
tm.assert_series_equal(ts[l_slc], expected)
786759
tm.assert_series_equal(ts.loc[l_slc], expected)
787-
tm.assert_series_equal(ts.loc[l_slc], expected)
788760

789761
keystr1 = str(index[9])
790762
keystr2 = str(index[13])

pandas/tests/series/indexing/test_numeric.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,24 @@
44
import pandas._testing as tm
55

66

7-
def test_slice_float64():
7+
def test_slice_float64(frame_or_series):
88
values = np.arange(10.0, 50.0, 2)
99
index = Index(values)
1010

1111
start, end = values[[5, 15]]
1212

13-
s = Series(np.random.randn(20), index=index)
13+
data = np.random.randn(20, 3)
14+
if frame_or_series is not DataFrame:
15+
data = data[:, 0]
1416

15-
result = s[start:end]
16-
expected = s.iloc[5:16]
17-
tm.assert_series_equal(result, expected)
18-
19-
result = s.loc[start:end]
20-
tm.assert_series_equal(result, expected)
21-
22-
df = DataFrame(np.random.randn(20, 3), index=index)
17+
obj = frame_or_series(data, index=index)
2318

24-
result = df[start:end]
25-
expected = df.iloc[5:16]
26-
tm.assert_frame_equal(result, expected)
19+
result = obj[start:end]
20+
expected = obj.iloc[5:16]
21+
tm.assert_equal(result, expected)
2722

28-
result = df.loc[start:end]
29-
tm.assert_frame_equal(result, expected)
23+
result = obj.loc[start:end]
24+
tm.assert_equal(result, expected)
3025

3126

3227
def test_getitem_setitem_slice_bug():

pandas/tests/series/indexing/test_setitem.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
MultiIndex,
1010
NaT,
1111
Series,
12+
Timedelta,
1213
Timestamp,
1314
date_range,
1415
period_range,
@@ -508,6 +509,28 @@ def test_setitem_empty_series_timestamp_preserves_dtype(self):
508509
result = series["timestamp"]
509510
assert result == expected
510511

512+
@pytest.mark.parametrize(
513+
"td",
514+
[
515+
Timedelta("9 days"),
516+
Timedelta("9 days").to_timedelta64(),
517+
Timedelta("9 days").to_pytimedelta(),
518+
],
519+
)
520+
def test_append_timedelta_does_not_cast(self, td):
521+
# GH#22717 inserting a Timedelta should _not_ cast to int64
522+
expected = Series(["x", td], index=[0, "td"], dtype=object)
523+
524+
ser = Series(["x"])
525+
ser["td"] = td
526+
tm.assert_series_equal(ser, expected)
527+
assert isinstance(ser["td"], Timedelta)
528+
529+
ser = Series(["x"])
530+
ser.loc["td"] = Timedelta("9 days")
531+
tm.assert_series_equal(ser, expected)
532+
assert isinstance(ser["td"], Timedelta)
533+
511534

512535
def test_setitem_scalar_into_readonly_backing_data():
513536
# GH#14359: test that you cannot mutate a read only buffer

0 commit comments

Comments
 (0)