Skip to content

BUG: BooleanArray+pd.NA altering array inplace #45421

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

Merged
merged 6 commits into from
Jan 19, 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Timezones

Numeric
^^^^^^^
-
- Bug in operations with array-likes with ``dtype="boolean"`` and :attr:`NA` incorrectly altering the array in-place (:issue:`45421`)
-

Conversion
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ def _arith_method(self, other, op):
if mask is None:
mask = self._mask
if other is libmissing.NA:
mask |= True
# GH#45421 don't alter inplace
mask = mask | True
else:
mask = self._mask | mask

Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/arrays/masked/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,22 @@ def test_array_scalar_like_equivalence(data, all_arithmetic_operators):
tm.assert_extension_array_equal(result, expected)


def test_array_NA(data, all_arithmetic_operators, request):
def test_array_NA(data, all_arithmetic_operators):
data, _ = data
op = tm.get_op_from_name(all_arithmetic_operators)
check_skip(data, all_arithmetic_operators)

scalar = pd.NA
scalar_array = pd.array([pd.NA] * len(data), dtype=data.dtype)

mask = data._mask.copy()
result = op(data, scalar)
# GH#45421 check op doesn't alter data._mask inplace
tm.assert_numpy_array_equal(mask, data._mask)

expected = op(data, scalar_array)
tm.assert_numpy_array_equal(mask, data._mask)

tm.assert_extension_array_equal(result, expected)


Expand Down