diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 3f898ca23bd6f..aff30d113438e 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -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*** -- - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index ca19670f37710..19aa4e173215e 100644 --- a/pandas/_libs/testing.pyx +++ b/pandas/_libs/testing.pyx @@ -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) diff --git a/pandas/tests/util/test_assert_almost_equal.py b/pandas/tests/util/test_assert_almost_equal.py index 0167e105efa23..5c50e5ad83967 100644 --- a/pandas/tests/util/test_assert_almost_equal.py +++ b/pandas/tests/util/test_assert_almost_equal.py @@ -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}) diff --git a/pandas/tests/util/test_assert_frame_equal.py b/pandas/tests/util/test_assert_frame_equal.py index a6e29e243b0c8..2d3b47cd2e994 100644 --- a/pandas/tests/util/test_assert_frame_equal.py +++ b/pandas/tests/util/test_assert_frame_equal.py @@ -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)