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 3 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
12 changes: 12 additions & 0 deletions pandas/tests/arrays/boolean/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ def test_op_int8(left_array, right_array, opname):
tm.assert_extension_array_equal(result, expected)


def test_boolean_array_add_pdna():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe can move this test to arrays/masked/test_arithmetic.py to have it for the integer and floating dtypes at the same time as well (those don't have this bug, but I don't directly see a test for it)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, updated

# GH#45421
data = pd.array([True, False])

res = data + pd.NA
assert res.isna().all()

# check we didn't alter data._mask inplace
expected = pd.array([True, False])
tm.assert_extension_array_equal(data, expected)


# Test generic characteristics / errors
# -----------------------------------------------------------------------------

Expand Down