From 67b4a2c64e0b0090e025115abc95ac02bd86c1d9 Mon Sep 17 00:00:00 2001 From: phofl Date: Tue, 26 Jan 2021 22:00:04 +0100 Subject: [PATCH 1/2] Fix assert bug --- pandas/_testing/asserters.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index b20cc0897ec18..660a3d019c546 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -968,14 +968,24 @@ def assert_series_equal( assert_attr_equal("dtype", left, right, obj=f"Attributes of {obj}") if check_exact and is_numeric_dtype(left.dtype) and is_numeric_dtype(right.dtype): + left_values = left._values + right_values = right._values # Only check exact if dtype is numeric - assert_numpy_array_equal( - left._values, - right._values, - check_dtype=check_dtype, - obj=str(obj), - index_values=np.asarray(left.index), - ) + if is_extension_array_dtype(left_values): + assert_extension_array_equal( + left_values, + right_values, + check_dtype=check_dtype, + index_values=np.asarray(left.index), + ) + else: + assert_numpy_array_equal( + left_values, + right_values, + check_dtype=check_dtype, + obj=str(obj), + index_values=np.asarray(left.index), + ) elif check_datetimelike_compat and ( needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype) ): From 5cc54ad3949f77af440314a1d9a68f9bf29ed470 Mon Sep 17 00:00:00 2001 From: phofl Date: Tue, 26 Jan 2021 22:21:46 +0100 Subject: [PATCH 2/2] Add tests --- doc/source/whatsnew/v1.2.2.rst | 2 +- pandas/_testing/asserters.py | 4 +++- pandas/tests/util/test_assert_frame_equal.py | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.2.2.rst b/doc/source/whatsnew/v1.2.2.rst index 5e96587a326d9..53fb323c890e2 100644 --- a/doc/source/whatsnew/v1.2.2.rst +++ b/doc/source/whatsnew/v1.2.2.rst @@ -14,7 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- +- 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`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index 660a3d019c546..494d9ac60dd96 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -971,7 +971,9 @@ def assert_series_equal( left_values = left._values right_values = right._values # Only check exact if dtype is numeric - if is_extension_array_dtype(left_values): + if is_extension_array_dtype(left_values) and is_extension_array_dtype( + right_values + ): assert_extension_array_equal( left_values, right_values, diff --git a/pandas/tests/util/test_assert_frame_equal.py b/pandas/tests/util/test_assert_frame_equal.py index a594751808532..bf1311460a5f5 100644 --- a/pandas/tests/util/test_assert_frame_equal.py +++ b/pandas/tests/util/test_assert_frame_equal.py @@ -305,3 +305,19 @@ def test_assert_frame_equal_columns_mixed_dtype(): # GH#39168 df = DataFrame([[0, 1, 2]], columns=["foo", "bar", 42], index=[1, "test", 2]) tm.assert_frame_equal(df, df, check_like=True) + + +def test_frame_equal_extension_dtype(frame_or_series, any_nullable_numeric_dtype): + # GH#39410 + obj = frame_or_series([1, 2], dtype=any_nullable_numeric_dtype) + tm.assert_equal(obj, obj, check_exact=True) + + +@pytest.mark.parametrize("indexer", [(0, 1), (1, 0)]) +def test_frame_equal_mixed_dtypes(frame_or_series, any_nullable_numeric_dtype, indexer): + dtypes = (any_nullable_numeric_dtype, "int64") + obj1 = frame_or_series([1, 2], dtype=dtypes[indexer[0]]) + obj2 = frame_or_series([1, 2], dtype=dtypes[indexer[1]]) + msg = r'(Series|DataFrame.iloc\[:, 0\] \(column name="0"\) classes) are different' + with pytest.raises(AssertionError, match=msg): + tm.assert_equal(obj1, obj2, check_exact=True, check_dtype=False)