From 9b5b64071cb7a4dfa48df96a28c681bedd8dc652 Mon Sep 17 00:00:00 2001 From: Jayendra Date: Sat, 11 Mar 2023 09:35:29 +0530 Subject: [PATCH 1/6] Added check for sets in assert_almost_equal --- pandas/_libs/testing.pyx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index ca19670f37710..aa8983b593a43 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 + return True + if isinstance(a, dict) or isinstance(b, dict): return assert_dict_equal(a, b) From 46d28a86dfbdb2c20f2a36302c741abca3274c6d Mon Sep 17 00:00:00 2001 From: Jayendra Date: Sat, 11 Mar 2023 09:46:16 +0530 Subject: [PATCH 2/6] Added a message for assertion fail --- pandas/_libs/testing.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index aa8983b593a43..19aa4e173215e 100644 --- a/pandas/_libs/testing.pyx +++ b/pandas/_libs/testing.pyx @@ -96,7 +96,7 @@ cpdef assert_almost_equal(a, b, robj = b if isinstance(a, set) or isinstance(b, set): - assert a==b + assert a == b, f"{a} != {b}" return True if isinstance(a, dict) or isinstance(b, dict): From 20f15859d721867e5395f9a0009a29cea3ebae45 Mon Sep 17 00:00:00 2001 From: Jayendra Date: Sat, 11 Mar 2023 11:37:46 +0530 Subject: [PATCH 3/6] Added testcases for sets in test_assert_almost_equal.py and test_assert_almost_equal.py --- pandas/tests/util/test_assert_almost_equal.py | 10 ++++++++++ pandas/tests/util/test_assert_frame_equal.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/pandas/tests/util/test_assert_almost_equal.py b/pandas/tests/util/test_assert_almost_equal.py index 0167e105efa23..ea11b097f1e9d 100644 --- a/pandas/tests/util/test_assert_almost_equal.py +++ b/pandas/tests/util/test_assert_almost_equal.py @@ -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}) diff --git a/pandas/tests/util/test_assert_frame_equal.py b/pandas/tests/util/test_assert_frame_equal.py index a6e29e243b0c8..4e5e38c9d3fe6 100644 --- a/pandas/tests/util/test_assert_frame_equal.py +++ b/pandas/tests/util/test_assert_frame_equal.py @@ -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) From 1e44b7e0afcfabbe8676033f26ed5f6a245dc756 Mon Sep 17 00:00:00 2001 From: Jayendra Date: Sat, 18 Mar 2023 08:40:12 +0530 Subject: [PATCH 4/6] Added issue in whatsnew, issue number for tests --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/tests/util/test_assert_almost_equal.py | 2 ++ pandas/tests/util/test_assert_frame_equal.py | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 4f6a6a8f0487b..e16fc4990465f 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -226,7 +226,7 @@ Other .. ***DO NOT USE THIS SECTION*** -- +- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`) - .. --------------------------------------------------------------------------- diff --git a/pandas/tests/util/test_assert_almost_equal.py b/pandas/tests/util/test_assert_almost_equal.py index ea11b097f1e9d..5c50e5ad83967 100644 --- a/pandas/tests/util/test_assert_almost_equal.py +++ b/pandas/tests/util/test_assert_almost_equal.py @@ -203,10 +203,12 @@ 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}) diff --git a/pandas/tests/util/test_assert_frame_equal.py b/pandas/tests/util/test_assert_frame_equal.py index 4e5e38c9d3fe6..2d3b47cd2e994 100644 --- a/pandas/tests/util/test_assert_frame_equal.py +++ b/pandas/tests/util/test_assert_frame_equal.py @@ -365,12 +365,14 @@ def test_assert_frame_equal_ts_column(): 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}]}) From aaee4328d691875be92276ec33e3f302ea0061de Mon Sep 17 00:00:00 2001 From: Jayendra Date: Sat, 18 Mar 2023 19:44:50 +0530 Subject: [PATCH 5/6] changed whatsnew --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index e16fc4990465f..009924f068823 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -223,10 +223,10 @@ Styler Other ^^^^^ +- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`) .. ***DO NOT USE THIS SECTION*** -- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`) - .. --------------------------------------------------------------------------- From ea33c6d0f2b6220aef3dbc576a0fd8249854226e Mon Sep 17 00:00:00 2001 From: Jayendra Date: Sat, 18 Mar 2023 19:52:11 +0530 Subject: [PATCH 6/6] changed whatsnew --- doc/source/whatsnew/v2.1.0.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 0249dd37f5204..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*** -- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`) - .. ---------------------------------------------------------------------------