Skip to content

Commit a474af5

Browse files
authored
BUG: BooleanArray+pd.NA altering array inplace (#45421)
1 parent b4e578d commit a474af5

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ Timezones
185185

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

191191
Conversion

pandas/core/arrays/boolean.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ def _arith_method(self, other, op):
494494
if mask is None:
495495
mask = self._mask
496496
if other is libmissing.NA:
497-
mask |= True
497+
# GH#45421 don't alter inplace
498+
mask = mask | True
498499
else:
499500
mask = self._mask | mask
500501

pandas/tests/arrays/masked/test_arithmetic.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,22 @@ def test_array_scalar_like_equivalence(data, all_arithmetic_operators):
4747
tm.assert_extension_array_equal(result, expected)
4848

4949

50-
def test_array_NA(data, all_arithmetic_operators, request):
50+
def test_array_NA(data, all_arithmetic_operators):
5151
data, _ = data
5252
op = tm.get_op_from_name(all_arithmetic_operators)
5353
check_skip(data, all_arithmetic_operators)
5454

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

58+
mask = data._mask.copy()
5859
result = op(data, scalar)
60+
# GH#45421 check op doesn't alter data._mask inplace
61+
tm.assert_numpy_array_equal(mask, data._mask)
62+
5963
expected = op(data, scalar_array)
64+
tm.assert_numpy_array_equal(mask, data._mask)
65+
6066
tm.assert_extension_array_equal(result, expected)
6167

6268

0 commit comments

Comments
 (0)