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

Backport PR #39971 on branch 1.2.x (REGR: Fix assignment bug for unary operators) #39990

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.2.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~

- Fixed regression in :meth:`~DataFrame.to_excel` raising ``KeyError`` when giving duplicate columns with ``columns`` attribute (:issue:`39695`)
- Fixed regression in :class:`IntegerArray` unary ops propagating mask on assignment (:issue:`39943`)
- Fixed regression in :meth:`DataFrame.__setitem__` not aligning :class:`DataFrame` on right-hand side for boolean indexer (:issue:`39931`)

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,13 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
super().__init__(values, mask, copy=copy)

def __neg__(self):
return type(self)(-self._data, self._mask)
return type(self)(-self._data, self._mask.copy())

def __pos__(self):
return self

def __abs__(self):
return type(self)(np.abs(self._data), self._mask)
return type(self)(np.abs(self._data), self._mask.copy())

@classmethod
def _from_sequence(
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __len__(self) -> int:
return len(self._data)

def __invert__(self: BaseMaskedArrayT) -> BaseMaskedArrayT:
return type(self)(~self._data, self._mask)
return type(self)(~self._data, self._mask.copy())

def to_numpy(
self, dtype=None, copy: bool = False, na_value: Scalar = lib.no_default
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/arrays/masked/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,16 @@ def test_error_len_mismatch(data, all_arithmetic_operators):
s = pd.Series(data)
with pytest.raises(ValueError, match="Lengths must match"):
op(s, other)


@pytest.mark.parametrize("op", ["__neg__", "__abs__", "__invert__"])
@pytest.mark.parametrize(
"values, dtype", [([1, 2, 3], "Int64"), ([True, False, True], "boolean")]
)
def test_unary_op_does_not_propagate_mask(op, values, dtype):
# https://github.com/pandas-dev/pandas/issues/39943
s = pd.Series(values, dtype=dtype)
result = getattr(s, op)()
expected = result.copy(deep=True)
s[0] = None
tm.assert_series_equal(result, expected)