Skip to content

Commit b5aec87

Browse files
assert_frame_equal for set (#51899)
* Added check for sets in assert_almost_equal * Added a message for assertion fail * Added testcases for sets in test_assert_almost_equal.py and test_assert_almost_equal.py * Added issue in whatsnew, issue number for tests * changed whatsnew * changed whatsnew
1 parent bcee6d6 commit b5aec87

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,10 @@ Styler
240240

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

245245
.. ***DO NOT USE THIS SECTION***
246246
247-
-
248247
-
249248

250249
.. ---------------------------------------------------------------------------

pandas/_libs/testing.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ cpdef assert_almost_equal(a, b,
9595
if robj is None:
9696
robj = b
9797

98+
if isinstance(a, set) or isinstance(b, set):
99+
assert a == b, f"{a} != {b}"
100+
return True
101+
98102
if isinstance(a, dict) or isinstance(b, dict):
99103
return assert_dict_equal(a, b)
100104

pandas/tests/util/test_assert_almost_equal.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ def test_assert_almost_equal_edge_case_ndarrays(left_dtype, right_dtype):
202202
)
203203

204204

205+
def test_assert_almost_equal_sets():
206+
# GH#51727
207+
_assert_almost_equal_both({1, 2, 3}, {1, 2, 3})
208+
209+
210+
def test_assert_almost_not_equal_sets():
211+
# GH#51727
212+
msg = r"{1, 2, 3} != {1, 2, 4}"
213+
with pytest.raises(AssertionError, match=msg):
214+
_assert_almost_equal_both({1, 2, 3}, {1, 2, 4})
215+
216+
205217
def test_assert_almost_equal_dicts():
206218
_assert_almost_equal_both({"a": 1, "b": 2}, {"a": 1, "b": 2})
207219

pandas/tests/util/test_assert_frame_equal.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,20 @@ def test_assert_frame_equal_ts_column():
362362
msg = r'DataFrame.iloc\[:, 0\] \(column name="a"\) values are different'
363363
with pytest.raises(AssertionError, match=msg):
364364
tm.assert_frame_equal(df1, df2)
365+
366+
367+
def test_assert_frame_equal_set():
368+
# GH#51727
369+
df1 = DataFrame({"set_column": [{1, 2, 3}, {4, 5, 6}]})
370+
df2 = DataFrame({"set_column": [{1, 2, 3}, {4, 5, 6}]})
371+
tm.assert_frame_equal(df1, df2)
372+
373+
374+
def test_assert_frame_equal_set_mismatch():
375+
# GH#51727
376+
df1 = DataFrame({"set_column": [{1, 2, 3}, {4, 5, 6}]})
377+
df2 = DataFrame({"set_column": [{1, 2, 3}, {4, 5, 7}]})
378+
379+
msg = r'DataFrame.iloc\[:, 0\] \(column name="set_column"\) values are different'
380+
with pytest.raises(AssertionError, match=msg):
381+
tm.assert_frame_equal(df1, df2)

0 commit comments

Comments
 (0)