Skip to content

Commit 201d263

Browse files
authored
BUG: Assert_frame_equal always raising AssertionError when comparing extension dtypes (pandas-dev#39423)
1 parent 9ad01af commit 201d263

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/asserters.py

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

970970
if check_exact and is_numeric_dtype(left.dtype) and is_numeric_dtype(right.dtype):
971+
left_values = left._values
972+
right_values = right._values
971973
# Only check exact if dtype is numeric
972-
assert_numpy_array_equal(
973-
left._values,
974-
right._values,
975-
check_dtype=check_dtype,
976-
obj=str(obj),
977-
index_values=np.asarray(left.index),
978-
)
974+
if is_extension_array_dtype(left_values) and is_extension_array_dtype(
975+
right_values
976+
):
977+
assert_extension_array_equal(
978+
left_values,
979+
right_values,
980+
check_dtype=check_dtype,
981+
index_values=np.asarray(left.index),
982+
)
983+
else:
984+
assert_numpy_array_equal(
985+
left_values,
986+
right_values,
987+
check_dtype=check_dtype,
988+
obj=str(obj),
989+
index_values=np.asarray(left.index),
990+
)
979991
elif check_datetimelike_compat and (
980992
needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype)
981993
):

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_nullable_numeric_dtype):
311+
# GH#39410
312+
obj = frame_or_series([1, 2], dtype=any_nullable_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_nullable_numeric_dtype, indexer):
318+
dtypes = (any_nullable_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)