Skip to content

Commit cda4544

Browse files
jbrockmendelnickleus27
authored andcommitted
TST: de-duplicate assert_slics_equivalent (pandas-dev#44415)
1 parent b948260 commit cda4544

File tree

5 files changed

+47
-45
lines changed

5 files changed

+47
-45
lines changed

pandas/_testing/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
assert_extension_array_equal,
8383
assert_frame_equal,
8484
assert_index_equal,
85+
assert_indexing_slices_equivalent,
8586
assert_interval_array_equal,
8687
assert_is_sorted,
8788
assert_is_valid_plot_return_object,

pandas/_testing/asserters.py

+14
Original file line numberDiff line numberDiff line change
@@ -1445,3 +1445,17 @@ def is_extension_array_dtype_and_needs_i8_conversion(left_dtype, right_dtype) ->
14451445
Related to issue #37609
14461446
"""
14471447
return is_extension_array_dtype(left_dtype) and needs_i8_conversion(right_dtype)
1448+
1449+
1450+
def assert_indexing_slices_equivalent(ser: Series, l_slc: slice, i_slc: slice):
1451+
"""
1452+
Check that ser.iloc[i_slc] matches ser.loc[l_slc] and, if applicable,
1453+
ser[l_slc].
1454+
"""
1455+
expected = ser.iloc[i_slc]
1456+
1457+
assert_series_equal(ser.loc[l_slc], expected)
1458+
1459+
if not ser.index.is_integer():
1460+
# For integer indices, .loc and plain getitem are position-based.
1461+
assert_series_equal(ser[l_slc], expected)

pandas/tests/indexing/multiindex/test_slice.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -702,32 +702,30 @@ def test_per_axis_per_level_setitem(self):
702702
tm.assert_frame_equal(df, expected)
703703

704704
def test_multiindex_label_slicing_with_negative_step(self):
705-
s = Series(
705+
ser = Series(
706706
np.arange(20), MultiIndex.from_product([list("abcde"), np.arange(4)])
707707
)
708708
SLC = pd.IndexSlice
709709

710-
def assert_slices_equivalent(l_slc, i_slc):
711-
tm.assert_series_equal(s.loc[l_slc], s.iloc[i_slc])
712-
tm.assert_series_equal(s[l_slc], s.iloc[i_slc])
710+
tm.assert_indexing_slices_equivalent(ser, SLC[::-1], SLC[::-1])
713711

714-
assert_slices_equivalent(SLC[::-1], SLC[::-1])
712+
tm.assert_indexing_slices_equivalent(ser, SLC["d"::-1], SLC[15::-1])
713+
tm.assert_indexing_slices_equivalent(ser, SLC[("d",)::-1], SLC[15::-1])
715714

716-
assert_slices_equivalent(SLC["d"::-1], SLC[15::-1])
717-
assert_slices_equivalent(SLC[("d",)::-1], SLC[15::-1])
715+
tm.assert_indexing_slices_equivalent(ser, SLC[:"d":-1], SLC[:11:-1])
716+
tm.assert_indexing_slices_equivalent(ser, SLC[:("d",):-1], SLC[:11:-1])
718717

719-
assert_slices_equivalent(SLC[:"d":-1], SLC[:11:-1])
720-
assert_slices_equivalent(SLC[:("d",):-1], SLC[:11:-1])
718+
tm.assert_indexing_slices_equivalent(ser, SLC["d":"b":-1], SLC[15:3:-1])
719+
tm.assert_indexing_slices_equivalent(ser, SLC[("d",):"b":-1], SLC[15:3:-1])
720+
tm.assert_indexing_slices_equivalent(ser, SLC["d":("b",):-1], SLC[15:3:-1])
721+
tm.assert_indexing_slices_equivalent(ser, SLC[("d",):("b",):-1], SLC[15:3:-1])
722+
tm.assert_indexing_slices_equivalent(ser, SLC["b":"d":-1], SLC[:0])
721723

722-
assert_slices_equivalent(SLC["d":"b":-1], SLC[15:3:-1])
723-
assert_slices_equivalent(SLC[("d",):"b":-1], SLC[15:3:-1])
724-
assert_slices_equivalent(SLC["d":("b",):-1], SLC[15:3:-1])
725-
assert_slices_equivalent(SLC[("d",):("b",):-1], SLC[15:3:-1])
726-
assert_slices_equivalent(SLC["b":"d":-1], SLC[:0])
727-
728-
assert_slices_equivalent(SLC[("c", 2)::-1], SLC[10::-1])
729-
assert_slices_equivalent(SLC[:("c", 2):-1], SLC[:9:-1])
730-
assert_slices_equivalent(SLC[("e", 0):("c", 2):-1], SLC[16:9:-1])
724+
tm.assert_indexing_slices_equivalent(ser, SLC[("c", 2)::-1], SLC[10::-1])
725+
tm.assert_indexing_slices_equivalent(ser, SLC[:("c", 2):-1], SLC[:9:-1])
726+
tm.assert_indexing_slices_equivalent(
727+
ser, SLC[("e", 0):("c", 2):-1], SLC[16:9:-1]
728+
)
731729

732730
def test_multiindex_slice_first_level(self):
733731
# GH 12697

pandas/tests/indexing/test_indexing.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -709,21 +709,17 @@ def run_tests(df, rhs, right_loc, right_iloc):
709709
def test_str_label_slicing_with_negative_step(self):
710710
SLC = pd.IndexSlice
711711

712-
def assert_slices_equivalent(l_slc, i_slc):
713-
tm.assert_series_equal(s.loc[l_slc], s.iloc[i_slc])
714-
715-
if not idx.is_integer:
716-
# For integer indices, .loc and plain getitem are position-based.
717-
tm.assert_series_equal(s[l_slc], s.iloc[i_slc])
718-
tm.assert_series_equal(s.loc[l_slc], s.iloc[i_slc])
719-
720712
for idx in [_mklbl("A", 20), np.arange(20) + 100, np.linspace(100, 150, 20)]:
721713
idx = Index(idx)
722-
s = Series(np.arange(20), index=idx)
723-
assert_slices_equivalent(SLC[idx[9] :: -1], SLC[9::-1])
724-
assert_slices_equivalent(SLC[: idx[9] : -1], SLC[:8:-1])
725-
assert_slices_equivalent(SLC[idx[13] : idx[9] : -1], SLC[13:8:-1])
726-
assert_slices_equivalent(SLC[idx[9] : idx[13] : -1], SLC[:0])
714+
ser = Series(np.arange(20), index=idx)
715+
tm.assert_indexing_slices_equivalent(ser, SLC[idx[9] :: -1], SLC[9::-1])
716+
tm.assert_indexing_slices_equivalent(ser, SLC[: idx[9] : -1], SLC[:8:-1])
717+
tm.assert_indexing_slices_equivalent(
718+
ser, SLC[idx[13] : idx[9] : -1], SLC[13:8:-1]
719+
)
720+
tm.assert_indexing_slices_equivalent(
721+
ser, SLC[idx[9] : idx[13] : -1], SLC[:0]
722+
)
727723

728724
def test_slice_with_zero_step_raises(self, indexer_sl, frame_or_series):
729725
obj = frame_or_series(np.arange(20), index=_mklbl("A", 20))

pandas/tests/series/indexing/test_indexing.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -338,26 +338,19 @@ def test_slice_with_zero_step_raises(index, frame_or_series, indexer_sli):
338338
],
339339
)
340340
def test_slice_with_negative_step(index):
341-
def assert_slices_equivalent(l_slc, i_slc):
342-
expected = ts.iloc[i_slc]
343-
344-
tm.assert_series_equal(ts[l_slc], expected)
345-
tm.assert_series_equal(ts.loc[l_slc], expected)
346-
347341
keystr1 = str(index[9])
348342
keystr2 = str(index[13])
349-
box = type(index[0])
350343

351-
ts = Series(np.arange(20), index)
344+
ser = Series(np.arange(20), index)
352345
SLC = IndexSlice
353346

354-
for key in [keystr1, box(keystr1)]:
355-
assert_slices_equivalent(SLC[key::-1], SLC[9::-1])
356-
assert_slices_equivalent(SLC[:key:-1], SLC[:8:-1])
347+
for key in [keystr1, index[9]]:
348+
tm.assert_indexing_slices_equivalent(ser, SLC[key::-1], SLC[9::-1])
349+
tm.assert_indexing_slices_equivalent(ser, SLC[:key:-1], SLC[:8:-1])
357350

358-
for key2 in [keystr2, box(keystr2)]:
359-
assert_slices_equivalent(SLC[key2:key:-1], SLC[13:8:-1])
360-
assert_slices_equivalent(SLC[key:key2:-1], SLC[0:0:-1])
351+
for key2 in [keystr2, index[13]]:
352+
tm.assert_indexing_slices_equivalent(ser, SLC[key2:key:-1], SLC[13:8:-1])
353+
tm.assert_indexing_slices_equivalent(ser, SLC[key:key2:-1], SLC[0:0:-1])
361354

362355

363356
def test_tuple_index():

0 commit comments

Comments
 (0)