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
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
10 changes: 10 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,16 @@ def test_assert_almost_equal_edge_case_ndarrays(left_dtype, right_dtype):
)


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


def test_assert_almost_not_equal_sets():
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
15 changes: 15 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,18 @@ 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():
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():
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)