Skip to content
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

BUG: frame.mask(foo, bar, inplace=True) with EAs incorrectly raising #45577

Merged
merged 3 commits into from
Jan 28, 2022
Merged
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.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Indexing
- 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__` when setting ``boolean`` dtype values containing ``NA`` incorrectly raising instead of casting to ``boolean`` dtype (:issue:`45462`)
- 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`)
- Bug in getting a column from a DataFrame with an object-dtype row index with datetime-like values: the resulting Series now preserves the exact object-dtype Index from the parent DataFrame (:issue:`42950`)
-

Expand Down
1 change: 1 addition & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/extension/base/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down