Skip to content

assert_frame_equal for set #51899

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 9 commits into from
Mar 20, 2023
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,10 @@ Styler

Other
^^^^^
-
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)

.. ***DO NOT USE THIS SECTION***

-
-

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ cpdef assert_almost_equal(a, b,
if robj is None:
robj = b

if isinstance(a, set) or isinstance(b, set):
assert a == b, f"{a} != {b}"
return True

if isinstance(a, dict) or isinstance(b, dict):
return assert_dict_equal(a, b)

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/util/test_assert_almost_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ def test_assert_almost_equal_edge_case_ndarrays(left_dtype, right_dtype):
)


def test_assert_almost_equal_sets():
# GH#51727
_assert_almost_equal_both({1, 2, 3}, {1, 2, 3})


def test_assert_almost_not_equal_sets():
# GH#51727
msg = r"{1, 2, 3} != {1, 2, 4}"
with pytest.raises(AssertionError, match=msg):
_assert_almost_equal_both({1, 2, 3}, {1, 2, 4})


def test_assert_almost_equal_dicts():
_assert_almost_equal_both({"a": 1, "b": 2}, {"a": 1, "b": 2})

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/util/test_assert_frame_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,20 @@ def test_assert_frame_equal_ts_column():
msg = r'DataFrame.iloc\[:, 0\] \(column name="a"\) values are different'
with pytest.raises(AssertionError, match=msg):
tm.assert_frame_equal(df1, df2)


def test_assert_frame_equal_set():
# GH#51727
df1 = DataFrame({"set_column": [{1, 2, 3}, {4, 5, 6}]})
df2 = DataFrame({"set_column": [{1, 2, 3}, {4, 5, 6}]})
tm.assert_frame_equal(df1, df2)


def test_assert_frame_equal_set_mismatch():
# GH#51727
df1 = DataFrame({"set_column": [{1, 2, 3}, {4, 5, 6}]})
df2 = DataFrame({"set_column": [{1, 2, 3}, {4, 5, 7}]})

msg = r'DataFrame.iloc\[:, 0\] \(column name="set_column"\) values are different'
with pytest.raises(AssertionError, match=msg):
tm.assert_frame_equal(df1, df2)