Skip to content

TST: de-duplicate assert_slics_equivalent #44415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
assert_extension_array_equal,
assert_frame_equal,
assert_index_equal,
assert_indexing_slices_equivalent,
assert_interval_array_equal,
assert_is_sorted,
assert_is_valid_plot_return_object,
Expand Down
14 changes: 14 additions & 0 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,3 +1444,17 @@ def is_extension_array_dtype_and_needs_i8_conversion(left_dtype, right_dtype) ->
Related to issue #37609
"""
return is_extension_array_dtype(left_dtype) and needs_i8_conversion(right_dtype)


def assert_indexing_slices_equivalent(ser: Series, l_slc: slice, i_slc: slice):
"""
Check that ser.iloc[i_slc] matches ser.loc[l_slc] and, if applicable,
ser[l_slc].
"""
expected = ser.iloc[i_slc]

assert_series_equal(ser.loc[l_slc], expected)

if not ser.index.is_integer():
# For integer indices, .loc and plain getitem are position-based.
assert_series_equal(ser[l_slc], expected)
34 changes: 16 additions & 18 deletions pandas/tests/indexing/multiindex/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,32 +702,30 @@ def test_per_axis_per_level_setitem(self):
tm.assert_frame_equal(df, expected)

def test_multiindex_label_slicing_with_negative_step(self):
s = Series(
ser = Series(
np.arange(20), MultiIndex.from_product([list("abcde"), np.arange(4)])
)
SLC = pd.IndexSlice

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

assert_slices_equivalent(SLC[::-1], SLC[::-1])
tm.assert_indexing_slices_equivalent(ser, SLC["d"::-1], SLC[15::-1])
tm.assert_indexing_slices_equivalent(ser, SLC[("d",)::-1], SLC[15::-1])

assert_slices_equivalent(SLC["d"::-1], SLC[15::-1])
assert_slices_equivalent(SLC[("d",)::-1], SLC[15::-1])
tm.assert_indexing_slices_equivalent(ser, SLC[:"d":-1], SLC[:11:-1])
tm.assert_indexing_slices_equivalent(ser, SLC[:("d",):-1], SLC[:11:-1])

assert_slices_equivalent(SLC[:"d":-1], SLC[:11:-1])
assert_slices_equivalent(SLC[:("d",):-1], SLC[:11:-1])
tm.assert_indexing_slices_equivalent(ser, SLC["d":"b":-1], SLC[15:3:-1])
tm.assert_indexing_slices_equivalent(ser, SLC[("d",):"b":-1], SLC[15:3:-1])
tm.assert_indexing_slices_equivalent(ser, SLC["d":("b",):-1], SLC[15:3:-1])
tm.assert_indexing_slices_equivalent(ser, SLC[("d",):("b",):-1], SLC[15:3:-1])
tm.assert_indexing_slices_equivalent(ser, SLC["b":"d":-1], SLC[:0])

assert_slices_equivalent(SLC["d":"b":-1], SLC[15:3:-1])
assert_slices_equivalent(SLC[("d",):"b":-1], SLC[15:3:-1])
assert_slices_equivalent(SLC["d":("b",):-1], SLC[15:3:-1])
assert_slices_equivalent(SLC[("d",):("b",):-1], SLC[15:3:-1])
assert_slices_equivalent(SLC["b":"d":-1], SLC[:0])

assert_slices_equivalent(SLC[("c", 2)::-1], SLC[10::-1])
assert_slices_equivalent(SLC[:("c", 2):-1], SLC[:9:-1])
assert_slices_equivalent(SLC[("e", 0):("c", 2):-1], SLC[16:9:-1])
tm.assert_indexing_slices_equivalent(ser, SLC[("c", 2)::-1], SLC[10::-1])
tm.assert_indexing_slices_equivalent(ser, SLC[:("c", 2):-1], SLC[:9:-1])
tm.assert_indexing_slices_equivalent(
ser, SLC[("e", 0):("c", 2):-1], SLC[16:9:-1]
)

def test_multiindex_slice_first_level(self):
# GH 12697
Expand Down
22 changes: 9 additions & 13 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,21 +709,17 @@ def run_tests(df, rhs, right_loc, right_iloc):
def test_str_label_slicing_with_negative_step(self):
SLC = pd.IndexSlice

def assert_slices_equivalent(l_slc, i_slc):
tm.assert_series_equal(s.loc[l_slc], s.iloc[i_slc])

if not idx.is_integer:
# For integer indices, .loc and plain getitem are position-based.
tm.assert_series_equal(s[l_slc], s.iloc[i_slc])
tm.assert_series_equal(s.loc[l_slc], s.iloc[i_slc])

for idx in [_mklbl("A", 20), np.arange(20) + 100, np.linspace(100, 150, 20)]:
idx = Index(idx)
s = Series(np.arange(20), index=idx)
assert_slices_equivalent(SLC[idx[9] :: -1], SLC[9::-1])
assert_slices_equivalent(SLC[: idx[9] : -1], SLC[:8:-1])
assert_slices_equivalent(SLC[idx[13] : idx[9] : -1], SLC[13:8:-1])
assert_slices_equivalent(SLC[idx[9] : idx[13] : -1], SLC[:0])
ser = Series(np.arange(20), index=idx)
tm.assert_indexing_slices_equivalent(ser, SLC[idx[9] :: -1], SLC[9::-1])
tm.assert_indexing_slices_equivalent(ser, SLC[: idx[9] : -1], SLC[:8:-1])
tm.assert_indexing_slices_equivalent(
ser, SLC[idx[13] : idx[9] : -1], SLC[13:8:-1]
)
tm.assert_indexing_slices_equivalent(
ser, SLC[idx[9] : idx[13] : -1], SLC[:0]
)

def test_slice_with_zero_step_raises(self, indexer_sl, frame_or_series):
obj = frame_or_series(np.arange(20), index=_mklbl("A", 20))
Expand Down
21 changes: 7 additions & 14 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,26 +338,19 @@ def test_slice_with_zero_step_raises(index, frame_or_series, indexer_sli):
],
)
def test_slice_with_negative_step(index):
def assert_slices_equivalent(l_slc, i_slc):
expected = ts.iloc[i_slc]

tm.assert_series_equal(ts[l_slc], expected)
tm.assert_series_equal(ts.loc[l_slc], expected)

keystr1 = str(index[9])
keystr2 = str(index[13])
box = type(index[0])

ts = Series(np.arange(20), index)
ser = Series(np.arange(20), index)
SLC = IndexSlice

for key in [keystr1, box(keystr1)]:
assert_slices_equivalent(SLC[key::-1], SLC[9::-1])
assert_slices_equivalent(SLC[:key:-1], SLC[:8:-1])
for key in [keystr1, index[9]]:
tm.assert_indexing_slices_equivalent(ser, SLC[key::-1], SLC[9::-1])
tm.assert_indexing_slices_equivalent(ser, SLC[:key:-1], SLC[:8:-1])

for key2 in [keystr2, box(keystr2)]:
assert_slices_equivalent(SLC[key2:key:-1], SLC[13:8:-1])
assert_slices_equivalent(SLC[key:key2:-1], SLC[0:0:-1])
for key2 in [keystr2, index[13]]:
tm.assert_indexing_slices_equivalent(ser, SLC[key2:key:-1], SLC[13:8:-1])
tm.assert_indexing_slices_equivalent(ser, SLC[key:key2:-1], SLC[0:0:-1])


def test_tuple_index():
Expand Down