Skip to content

REGR: AttributeError: 'bool' object has no attribute 'to_numpy' in "mask_missing" #48313

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

Closed
Closed
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/v1.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Fixed regressions
- Fixed regression in :meth:`DatetimeIndex.intersection` when the :class:`DatetimeIndex` has dates crossing daylight savings time (:issue:`46702`)
- Fixed regression in :func:`merge` throwing an error when passing a :class:`Series` with a multi-level name (:issue:`47946`)
- Fixed regression in :meth:`DataFrame.eval` creating a copy when updating inplace (:issue:`47449`)
- Fixed regression where ``AttributeError`` may be raised when using ``pd.NA`` with ``object`` dtype (:issue:`47101`)
- Fixed regression where getting a row using :meth:`DataFrame.iloc` with :class:`SparseDtype` would raise (:issue:`46406`)

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Any,
cast,
)
import warnings

import numpy as np

Expand Down Expand Up @@ -92,8 +93,11 @@ def mask_missing(arr: ArrayLike, values_to_mask) -> npt.NDArray[np.bool_]:
# GH#29553 prevent numpy deprecation warnings
pass
else:
new_mask = arr == x
if not isinstance(new_mask, np.ndarray):
# GH#47101 we may get a scalar new_mask if elementwise comparison fails
with warnings.catch_warnings():
warnings.simplefilter("ignore", FutureWarning)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a condition under which we can remove this check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume when the numpy behaviour changes and a FutureWarning is no longer given.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the warning was not being suppressed in 1.3.5 so not strictly needed for the regression fix.

new_mask = arr == x
if not isinstance(new_mask, (np.ndarray, bool)):
# usually BooleanArray
new_mask = new_mask.to_numpy(dtype=bool, na_value=False)
mask |= new_mask
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,13 @@ def test_replace_list_with_mixed_type(
result = obj.replace(box(to_replace), value)
tm.assert_equal(result, expected)

def test_replace_object_dtype_with_pandas_na(self):
# GH#47101
df = DataFrame({"d": [pd.NA]})
with tm.assert_produces_warning(None):
result = df.replace("", pd.NA)
tm.assert_frame_equal(result, df)


class TestDataFrameReplaceRegex:
@pytest.mark.parametrize(
Expand Down