diff --git a/doc/source/whatsnew/v1.4.4.rst b/doc/source/whatsnew/v1.4.4.rst index d438c5705f2df..70054d4bb5467 100644 --- a/doc/source/whatsnew/v1.4.4.rst +++ b/doc/source/whatsnew/v1.4.4.rst @@ -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`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 6005e11efbac4..7c24de47a9515 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -12,6 +12,7 @@ Any, cast, ) +import warnings import numpy as np @@ -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) + 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 diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 177f3ec1b4504..bd451e6c0ce1a 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -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(