Skip to content

BUG: Fix assert_frame_equal dtype handling when check_dtype=False (#61473) #61525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ Sparse
ExtensionArray
^^^^^^^^^^^^^^
- Bug in :class:`Categorical` when constructing with an :class:`Index` with :class:`ArrowDtype` (:issue:`60563`)
- 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`)
- Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`)
- Bug in :meth:`ArrowExtensionArray.factorize` where NA values were dropped when input was dictionary-encoded even when dropna was set to False(:issue:`60567`)
- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`)
Expand Down
29 changes: 22 additions & 7 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ def assert_series_equal(
pass
else:
assert_attr_equal("dtype", left, right, obj=f"Attributes of {obj}")

if check_exact:
left_values = left._values
right_values = right._values
Expand All @@ -1030,11 +1031,18 @@ def assert_series_equal(
)
else:
# convert both to NumPy if not, check_dtype would raise earlier
lv, rv = left_values, right_values
if isinstance(left_values, ExtensionArray):
lv = left_values.to_numpy()
if isinstance(right_values, ExtensionArray):
rv = right_values.to_numpy()
lv, rv = left._values, right._values
if not check_dtype and not (
isinstance(lv, ExtensionArray) and isinstance(rv, ExtensionArray)
):
lv = left.to_numpy(dtype="object")
rv = right.to_numpy(dtype="object")
else:
if isinstance(lv, ExtensionArray):
lv = rv.to_numpy()
if isinstance(rv, ExtensionArray):
rv = rv.to_numpy()

assert_numpy_array_equal(
lv,
rv,
Expand Down Expand Up @@ -1105,9 +1113,16 @@ def assert_series_equal(
obj=str(obj),
)
else:
lv, rv = left._values, right._values
if not check_dtype and not (
isinstance(left._values, ExtensionArray)
and isinstance(right._values, ExtensionArray)
):
lv = left.to_numpy(dtype="object")
rv = right.to_numpy(dtype="object")
_testing.assert_almost_equal(
left._values,
right._values,
lv,
rv,
rtol=rtol,
atol=atol,
check_dtype=bool(check_dtype),
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/util/test_assert_frame_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,11 @@ def test_assert_frame_equal_set_mismatch():
msg = r'DataFrame.iloc\[:, 0\] \(column name="set_column"\) values are different'
with pytest.raises(AssertionError, match=msg):
tm.assert_frame_equal(df1, df2)


def test_assert_frame_equal_with_pdNA_and_check_dtype_false():
# GH#61473
df1 = DataFrame({"x": pd.Series([pd.NA], dtype="Int32")})
df2 = DataFrame({"x": pd.Series([pd.NA], dtype="object")})

tm.assert_frame_equal(df1, df2, check_dtype=False)
Loading