Skip to content

Commit 3577b05

Browse files
committed
BUG: Fix assert_frame_equal dtype handling when check_dtype=False (#61473)
1 parent cfe54bd commit 3577b05

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ Sparse
857857
ExtensionArray
858858
^^^^^^^^^^^^^^
859859
- Bug in :class:`Categorical` when constructing with an :class:`Index` with :class:`ArrowDtype` (:issue:`60563`)
860+
- Bug in :func:`assert_frame_equal` with ``check_dtype=False`` that failed when comparing columns containing ``pd.NA`` with ``Int32`` and ``object`` dtypes. Now handles such comparisons by coercing to ``dtype="object"`` internally. (:issue:`61473`)
860861
- Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`)
861862
- Bug in :meth:`ArrowExtensionArray.factorize` where NA values were dropped when input was dictionary-encoded even when dropna was set to False(:issue:`60567`)
862863
- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`)

pandas/_testing/asserters.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,9 +1014,13 @@ def assert_series_equal(
10141014
pass
10151015
else:
10161016
assert_attr_equal("dtype", left, right, obj=f"Attributes of {obj}")
1017-
if check_exact:
1017+
if not check_dtype:
1018+
left_values = left.to_numpy(dtype="object")
1019+
right_values = right.to_numpy(dtype="object")
1020+
else:
10181021
left_values = left._values
10191022
right_values = right._values
1023+
if check_exact:
10201024
# Only check exact if dtype is numeric
10211025
if isinstance(left_values, ExtensionArray) and isinstance(
10221026
right_values, ExtensionArray
@@ -1051,10 +1055,10 @@ def assert_series_equal(
10511055

10521056
# datetimelike may have different objects (e.g. datetime.datetime
10531057
# vs Timestamp) but will compare equal
1054-
if not Index(left._values).equals(Index(right._values)):
1058+
if not Index(left_values).equals(Index(right_values)):
10551059
msg = (
1056-
f"[datetimelike_compat=True] {left._values} "
1057-
f"is not equal to {right._values}."
1060+
f"[datetimelike_compat=True] {left_values} "
1061+
f"is not equal to {right_values}."
10581062
)
10591063
raise AssertionError(msg)
10601064
elif isinstance(left.dtype, IntervalDtype) and isinstance(
@@ -1065,8 +1069,8 @@ def assert_series_equal(
10651069
right.dtype, CategoricalDtype
10661070
):
10671071
_testing.assert_almost_equal(
1068-
left._values,
1069-
right._values,
1072+
left_values,
1073+
right_values,
10701074
rtol=rtol,
10711075
atol=atol,
10721076
check_dtype=bool(check_dtype),
@@ -1077,8 +1081,8 @@ def assert_series_equal(
10771081
right.dtype, ExtensionDtype
10781082
):
10791083
assert_extension_array_equal(
1080-
left._values,
1081-
right._values,
1084+
left_values,
1085+
right_values,
10821086
rtol=rtol,
10831087
atol=atol,
10841088
check_dtype=check_dtype,

pandas/tests/util/test_assert_frame_equal.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,11 @@ def test_assert_frame_equal_set_mismatch():
395395
msg = r'DataFrame.iloc\[:, 0\] \(column name="set_column"\) values are different'
396396
with pytest.raises(AssertionError, match=msg):
397397
tm.assert_frame_equal(df1, df2)
398+
399+
400+
def test_assert_frame_equal_with_pdNA_and_check_dtype_false():
401+
# GH#61473
402+
df1 = DataFrame({"x": pd.Series([pd.NA], dtype="Int32")})
403+
df2 = DataFrame({"x": pd.Series([pd.NA], dtype="object")})
404+
405+
tm.assert_frame_equal(df1, df2, check_dtype=False)

0 commit comments

Comments
 (0)