Skip to content

BUG: Fix assert_frame_equal with check_dtype=False for pd.NA with different dtypes #62092

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 @@ -955,6 +955,7 @@ Other
^^^^^
- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
- Bug in :class:`Series` ignoring errors when trying to convert :class:`Series` input data to the given ``dtype`` (:issue:`60728`)
- Bug in :func:`assert_frame_equal` with ``check_dtype=False`` failing to compare DataFrames containing ``pd.NA`` that differ only in dtype (``object`` vs ``Int32``). (:issue:`61473`)
- Bug in :func:`eval` on :class:`ExtensionArray` on including division ``/`` failed with a ``TypeError``. (:issue:`58748`)
- Bug in :func:`eval` where method calls on binary operations like ``(x + y).dropna()`` would raise ``AttributeError: 'BinOp' object has no attribute 'value'`` (:issue:`61175`)
- Bug in :func:`eval` where the names of the :class:`Series` were not preserved when using ``engine="numexpr"``. (:issue:`10239`)
Expand Down
14 changes: 10 additions & 4 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,16 @@ 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()
if check_dtype:
if isinstance(left_values, ExtensionArray):
lv = left_values.to_numpy()
if isinstance(right_values, ExtensionArray):
rv = right_values.to_numpy()
else:
if isinstance(left_values, ExtensionArray):
lv = left_values.to_numpy(dtype="object")
if isinstance(right_values, ExtensionArray):
rv = right_values.to_numpy(dtype="object")
assert_numpy_array_equal(
lv,
rv,
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_pdNa_ignore_dtype():
# 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