Skip to content

Commit 62a46fe

Browse files
authored
Backport PR #39423: BUG: Assert_frame_equal always raising AssertionError when comparing extension dtypes (#39449)
1 parent 06b4887 commit 62a46fe

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

doc/source/whatsnew/v1.2.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed regression in :meth:`~DataFrame.to_pickle` failing to create bz2/xz compressed pickle files with ``protocol=5`` (:issue:`39002`)
18+
- Fixed regression in :func:`pandas.testing.assert_series_equal` and :func:`pandas.testing.assert_frame_equal` always raising ``AssertionError`` when comparing extension dtypes (:issue:`39410`)
1819
-
1920

2021
.. ---------------------------------------------------------------------------

pandas/_testing.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -1402,14 +1402,26 @@ def assert_series_equal(
14021402
assert_attr_equal("dtype", left, right, obj=f"Attributes of {obj}")
14031403

14041404
if check_exact and is_numeric_dtype(left.dtype) and is_numeric_dtype(right.dtype):
1405+
left_values = left._values
1406+
right_values = right._values
14051407
# Only check exact if dtype is numeric
1406-
assert_numpy_array_equal(
1407-
left._values,
1408-
right._values,
1409-
check_dtype=check_dtype,
1410-
obj=str(obj),
1411-
index_values=np.asarray(left.index),
1412-
)
1408+
if is_extension_array_dtype(left_values) and is_extension_array_dtype(
1409+
right_values
1410+
):
1411+
assert_extension_array_equal(
1412+
left_values,
1413+
right_values,
1414+
check_dtype=check_dtype,
1415+
index_values=np.asarray(left.index),
1416+
)
1417+
else:
1418+
assert_numpy_array_equal(
1419+
left_values,
1420+
right_values,
1421+
check_dtype=check_dtype,
1422+
obj=str(obj),
1423+
index_values=np.asarray(left.index),
1424+
)
14131425
elif check_datetimelike_compat and (
14141426
needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype)
14151427
):

pandas/tests/util/test_assert_frame_equal.py

+16
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,19 @@ def test_assert_frame_equal_columns_mixed_dtype():
305305
# GH#39168
306306
df = DataFrame([[0, 1, 2]], columns=["foo", "bar", 42], index=[1, "test", 2])
307307
tm.assert_frame_equal(df, df, check_like=True)
308+
309+
310+
def test_frame_equal_extension_dtype(frame_or_series, any_numeric_dtype):
311+
# GH#39410
312+
obj = frame_or_series([1, 2], dtype=any_numeric_dtype)
313+
tm.assert_equal(obj, obj, check_exact=True)
314+
315+
316+
@pytest.mark.parametrize("indexer", [(0, 1), (1, 0)])
317+
def test_frame_equal_mixed_dtypes(frame_or_series, any_numeric_dtype, indexer):
318+
dtypes = (any_numeric_dtype, "int64")
319+
obj1 = frame_or_series([1, 2], dtype=dtypes[indexer[0]])
320+
obj2 = frame_or_series([1, 2], dtype=dtypes[indexer[1]])
321+
msg = r'(Series|DataFrame.iloc\[:, 0\] \(column name="0"\) classes) are different'
322+
with pytest.raises(AssertionError, match=msg):
323+
tm.assert_equal(obj1, obj2, check_exact=True, check_dtype=False)

0 commit comments

Comments
 (0)