From bc4306683a3be0a297a67b95e29784f5a7cba106 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 23 Jan 2022 12:29:23 -0800 Subject: [PATCH 1/2] BUG: frame.mask(foo, bar, inplace=True) with EAs incorrectly raising --- pandas/core/internals/blocks.py | 1 + pandas/tests/extension/base/methods.py | 12 +++++++++++- pandas/tests/frame/indexing/test_where.py | 6 ++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 3d4f53530b89c..ff56ed80f36f1 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1436,6 +1436,7 @@ def putmask(self, mask, new) -> list[Block]: values = self.values + new = self._maybe_squeeze_arg(new) mask = self._maybe_squeeze_arg(mask) try: diff --git a/pandas/tests/extension/base/methods.py b/pandas/tests/extension/base/methods.py index f5a18037d97eb..3fd50c08f5cc4 100644 --- a/pandas/tests/extension/base/methods.py +++ b/pandas/tests/extension/base/methods.py @@ -443,7 +443,8 @@ def test_where_series(self, data, na_value, as_frame): cls = type(data) a, b = data[:2] - ser = pd.Series(cls._from_sequence([a, a, b, b], dtype=data.dtype)) + orig = pd.Series(cls._from_sequence([a, a, b, b], dtype=data.dtype)) + ser = orig.copy() cond = np.array([True, True, False, False]) if as_frame: @@ -459,7 +460,13 @@ def test_where_series(self, data, na_value, as_frame): expected = expected.to_frame(name="a") self.assert_equal(result, expected) + ser.mask(~cond, inplace=True) + self.assert_equal(ser, expected) + # array other + ser = orig.copy() + if as_frame: + ser = ser.to_frame(name="a") cond = np.array([True, False, True, True]) other = cls._from_sequence([a, b, a, b], dtype=data.dtype) if as_frame: @@ -471,6 +478,9 @@ def test_where_series(self, data, na_value, as_frame): expected = expected.to_frame(name="a") self.assert_equal(result, expected) + ser.mask(~cond, other, inplace=True) + self.assert_equal(ser, expected) + @pytest.mark.parametrize("repeats", [0, 1, 2, [1, 2, 3]]) def test_repeat(self, data, repeats, as_series, use_numpy): arr = type(data)._from_sequence(data[:3], dtype=data.dtype) diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index 399318a6d6118..3e9bb6fca5558 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -812,6 +812,12 @@ def test_where_string_dtype(frame_or_series): ) tm.assert_equal(result, expected) + result = obj.mask(~filter_ser, filtered_obj) + tm.assert_equal(result, expected) + + obj.mask(~filter_ser, filtered_obj, inplace=True) + tm.assert_equal(result, expected) + def test_where_bool_comparison(): # GH 10336 From 7d9efaf765c0c2633b19cb8610d357b26c5075cf Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 23 Jan 2022 12:30:43 -0800 Subject: [PATCH 2/2] whatsnew --- doc/source/whatsnew/v1.5.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index b32416418a39f..1b9106f3e8e98 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -219,6 +219,7 @@ Indexing - Bug in :meth:`Series.__setitem__` with a non-integer :class:`Index` when using an integer key to set a value that cannot be set inplace where a ``ValueError`` was raised insead of casting to a common dtype (:issue:`45070`) - Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`) - Bug in :meth:`Series.__setitem__` where setting :attr:`NA` into a numeric-dtpye :class:`Series` would incorrectly upcast to object-dtype rather than treating the value as ``np.nan`` (:issue:`44199`) +- Bug in :meth:`DataFrame.mask` with ``inplace=True`` and ``ExtensionDtype`` columns incorrectly raising (:issue:`45577`) - Missing