Skip to content
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

TST/CLN: make equalContents more strict #56154

Merged
merged 2 commits into from
Nov 24, 2023
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
8 changes: 0 additions & 8 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,6 @@ def reset_display_options() -> None:
# Comparators


def equalContents(arr1, arr2) -> bool:
"""
Checks if the set of unique elements of arr1 and arr2 are equivalent.
"""
return frozenset(arr1) == frozenset(arr2)


def box_expected(expected, box_cls, transpose: bool = True):
"""
Helper function to wrap the expected output of a test in a given box_class.
Expand Down Expand Up @@ -1131,7 +1124,6 @@ def shares_memory(left, right) -> bool:
"EMPTY_STRING_PATTERN",
"ENDIAN",
"ensure_clean",
"equalContents",
"external_error_raised",
"FLOAT_EA_DTYPES",
"FLOAT_NUMPY_DTYPES",
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_getitem(self, float_frame):
# Column access
for _, series in sl.items():
assert len(series.index) == 20
assert tm.equalContents(series.index, sl.index)
tm.assert_index_equal(series.index, sl.index)

for key, _ in float_frame._series.items():
assert float_frame[key] is not None
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_combine_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_combine_first(self, float_frame):
combined = head.combine_first(tail)
reordered_frame = float_frame.reindex(combined.index)
tm.assert_frame_equal(combined, reordered_frame)
assert tm.equalContents(combined.columns, float_frame.columns)
tm.assert_index_equal(combined.columns, float_frame.columns)
tm.assert_series_equal(combined["A"], reordered_frame["A"])

# same index
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/methods/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def test_reindex(self, float_frame, using_copy_on_write):
assert np.isnan(val)

for col, series in newFrame.items():
assert tm.equalContents(series.index, newFrame.index)
tm.assert_index_equal(series.index, newFrame.index)
emptyFrame = float_frame.reindex(Index([]))
assert len(emptyFrame.index) == 0

Expand All @@ -642,7 +642,7 @@ def test_reindex(self, float_frame, using_copy_on_write):
assert np.isnan(val)

for col, series in nonContigFrame.items():
assert tm.equalContents(series.index, nonContigFrame.index)
tm.assert_index_equal(series.index, nonContigFrame.index)

# corner cases

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_items_names(self, float_string_frame):
assert v.name == k

def test_iter(self, float_frame):
assert tm.equalContents(list(float_frame), float_frame.columns)
assert list(float_frame) == list(float_frame.columns)

def test_iterrows(self, float_frame, float_string_frame):
for k, v in float_frame.iterrows():
Expand Down
13 changes: 10 additions & 3 deletions pandas/tests/indexes/base_class/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
from pandas.core.algorithms import safe_sort


def equal_contents(arr1, arr2) -> bool:
"""
Checks if the set of unique elements of arr1 and arr2 are equivalent.
"""
return frozenset(arr1) == frozenset(arr2)


class TestIndexSetOps:
@pytest.mark.parametrize(
"method", ["union", "intersection", "difference", "symmetric_difference"]
Expand Down Expand Up @@ -71,7 +78,7 @@ def test_union_different_type_base(self, klass):

result = first.union(klass(second.values))

assert tm.equalContents(result, index)
assert equal_contents(result, index)

def test_union_sort_other_incomparable(self):
# https://github.com/pandas-dev/pandas/issues/24959
Expand Down Expand Up @@ -119,7 +126,7 @@ def test_intersection_different_type_base(self, klass, sort):
second = index[:3]

result = first.intersection(klass(second.values), sort=sort)
assert tm.equalContents(result, second)
assert equal_contents(result, second)

def test_intersection_nosort(self):
result = Index(["c", "b", "a"]).intersection(["b", "a"])
Expand Down Expand Up @@ -244,7 +251,7 @@ def test_union_name_preservation(
tm.assert_index_equal(union, expected)
else:
expected = Index(vals, name=expected_name)
tm.equalContents(union, expected)
tm.assert_index_equal(union.sort_values(), expected.sort_values())

@pytest.mark.parametrize(
"diff_type, expected",
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ def test_intersection2(self):
first = tm.makeDateIndex(10)
second = first[5:]
intersect = first.intersection(second)
assert tm.equalContents(intersect, second)
tm.assert_index_equal(intersect, second)

# GH 10149
cases = [klass(second.values) for klass in [np.array, Series, list]]
for case in cases:
result = first.intersection(case)
assert tm.equalContents(result, second)
tm.assert_index_equal(result, second)

third = Index(["a", "b", "c"])
result = first.intersection(third)
Expand Down
30 changes: 18 additions & 12 deletions pandas/tests/indexes/interval/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ def test_union(self, closed, sort):

expected = monotonic_index(0, 13, closed=closed)
result = index[::-1].union(other, sort=sort)
if sort is None:
if sort in (None, True):
tm.assert_index_equal(result, expected)
assert tm.equalContents(result, expected)
else:
tm.assert_index_equal(result.sort_values(), expected)

result = other[::-1].union(index, sort=sort)
if sort is None:
if sort in (None, True):
tm.assert_index_equal(result, expected)
assert tm.equalContents(result, expected)
else:
tm.assert_index_equal(result.sort_values(), expected)

tm.assert_index_equal(index.union(index, sort=sort), index)
tm.assert_index_equal(index.union(index[:1], sort=sort), index)
Expand Down Expand Up @@ -65,14 +67,16 @@ def test_intersection(self, closed, sort):

expected = monotonic_index(5, 11, closed=closed)
result = index[::-1].intersection(other, sort=sort)
if sort is None:
if sort in (None, True):
tm.assert_index_equal(result, expected)
assert tm.equalContents(result, expected)
else:
tm.assert_index_equal(result.sort_values(), expected)

result = other[::-1].intersection(index, sort=sort)
if sort is None:
if sort in (None, True):
tm.assert_index_equal(result, expected)
assert tm.equalContents(result, expected)
else:
tm.assert_index_equal(result.sort_values(), expected)

tm.assert_index_equal(index.intersection(index, sort=sort), index)

Expand Down Expand Up @@ -148,16 +152,18 @@ def test_symmetric_difference(self, closed, sort):
index = monotonic_index(0, 11, closed=closed)
result = index[1:].symmetric_difference(index[:-1], sort=sort)
expected = IntervalIndex([index[0], index[-1]])
if sort is None:
if sort in (None, True):
tm.assert_index_equal(result, expected)
assert tm.equalContents(result, expected)
else:
tm.assert_index_equal(result.sort_values(), expected)

# GH 19101: empty result, same dtype
result = index.symmetric_difference(index, sort=sort)
expected = empty_index(dtype="int64", closed=closed)
if sort is None:
if sort in (None, True):
tm.assert_index_equal(result, expected)
assert tm.equalContents(result, expected)
else:
tm.assert_index_equal(result.sort_values(), expected)

# GH 19101: empty result, different dtypes
other = IntervalIndex.from_arrays(
Expand Down
15 changes: 8 additions & 7 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ def test_union(idx, sort):

the_union = piece1.union(piece2, sort=sort)

if sort is None:
tm.assert_index_equal(the_union, idx.sort_values())

assert tm.equalContents(the_union, idx)
if sort in (None, False):
tm.assert_index_equal(the_union.sort_values(), idx.sort_values())
else:
tm.assert_index_equal(the_union, idx)

# corner case, pass self or empty thing:
the_union = idx.union(idx, sort=sort)
Expand All @@ -258,7 +258,7 @@ def test_union(idx, sort):
tuples = idx.values
result = idx[:4].union(tuples[4:], sort=sort)
if sort is None:
tm.equalContents(result, idx)
tm.assert_index_equal(result.sort_values(), idx.sort_values())
else:
assert result.equals(idx)

Expand All @@ -284,9 +284,10 @@ def test_intersection(idx, sort):

the_int = piece1.intersection(piece2, sort=sort)

if sort is None:
if sort in (None, True):
tm.assert_index_equal(the_int, idx[3:5])
assert tm.equalContents(the_int, idx[3:5])
else:
tm.assert_index_equal(the_int.sort_values(), idx[3:5])

# corner case, pass self
the_int = idx.intersection(idx, sort=sort)
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/indexes/numeric/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ def test_symmetric_difference(self, sort):
index2 = Index([2, 3, 4, 1])
result = index1.symmetric_difference(index2, sort=sort)
expected = Index([5, 1])
assert tm.equalContents(result, expected)
if sort is not None:
tm.assert_index_equal(result, expected)
else:
tm.assert_index_equal(result, expected.sort_values())
assert result.name is None
if sort is None:
expected = expected.sort_values()
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/indexes/period/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ def test_union_misc(self, sort):

# not in order
result = _permute(index[:-5]).union(_permute(index[10:]), sort=sort)
if sort is None:
if sort is False:
tm.assert_index_equal(result.sort_values(), index)
else:
tm.assert_index_equal(result, index)
assert tm.equalContents(result, index)

# cast if different frequencies
index = period_range("1/1/2000", "1/20/2000", freq="D")
Expand All @@ -163,9 +164,10 @@ def test_intersection(self, sort):
left = _permute(index[:-5])
right = _permute(index[10:])
result = left.intersection(right, sort=sort)
if sort is None:
if sort is False:
tm.assert_index_equal(result.sort_values(), index[10:-5])
else:
tm.assert_index_equal(result, index[10:-5])
assert tm.equalContents(result, index[10:-5])

# cast if different frequencies
index = period_range("1/1/2000", "1/20/2000", freq="D")
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,10 @@ def test_union_dt_as_obj(self, simple_index):
first_cat = index.union(date_index)
second_cat = index.union(index)

appended = np.append(index, date_index.astype("O"))
appended = Index(np.append(index, date_index.astype("O")))

assert tm.equalContents(first_cat, appended)
assert tm.equalContents(second_cat, index)
tm.assert_index_equal(first_cat, appended)
tm.assert_index_equal(second_cat, index)
tm.assert_contains_all(index, first_cat)
tm.assert_contains_all(index, second_cat)
tm.assert_contains_all(date_index, first_cat)
Expand Down
Loading
Loading