Skip to content

Commit 51763f9

Browse files
authored
BUG: NA.__and__, __or__, __xor__ with np.bool_ objects (#61768)
1 parent 391107a commit 51763f9

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ Indexing
762762
Missing
763763
^^^^^^^
764764
- Bug in :meth:`DataFrame.fillna` and :meth:`Series.fillna` that would ignore the ``limit`` argument on :class:`.ExtensionArray` dtypes (:issue:`58001`)
765+
- Bug in :meth:`NA.__and__`, :meth:`NA.__or__` and :meth:`NA.__xor__` when operating with ``np.bool_`` objects (:issue:`58427`)
765766
-
766767

767768
MultiIndex

pandas/_libs/missing.pyx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ class NAType(C_NAType):
471471
return False
472472
elif other is True or other is C_NA:
473473
return NA
474+
elif util.is_bool_object(other):
475+
if not other:
476+
return False
477+
return NA
474478
return NotImplemented
475479

476480
__rand__ = __and__
@@ -480,12 +484,16 @@ class NAType(C_NAType):
480484
return True
481485
elif other is False or other is C_NA:
482486
return NA
487+
elif util.is_bool_object(other):
488+
if not other:
489+
return NA
490+
return True
483491
return NotImplemented
484492

485493
__ror__ = __or__
486494

487495
def __xor__(self, other):
488-
if other is False or other is True or other is C_NA:
496+
if util.is_bool_object(other) or other is C_NA:
489497
return NA
490498
return NotImplemented
491499

pandas/tests/scalar/test_na_scalar.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ def test_logical_and():
167167
assert False & NA is False
168168
assert NA & NA is NA
169169

170+
# GH#58427
171+
assert NA & np.bool_(True) is NA
172+
assert np.bool_(True) & NA is NA
173+
assert NA & np.bool_(False) is False
174+
assert np.bool_(False) & NA is False
175+
170176
msg = "unsupported operand type"
171177
with pytest.raises(TypeError, match=msg):
172178
NA & 5
@@ -179,6 +185,12 @@ def test_logical_or():
179185
assert False | NA is NA
180186
assert NA | NA is NA
181187

188+
# GH#58427
189+
assert NA | np.bool_(True) is True
190+
assert np.bool_(True) | NA is True
191+
assert NA | np.bool_(False) is NA
192+
assert np.bool_(False) | NA is NA
193+
182194
msg = "unsupported operand type"
183195
with pytest.raises(TypeError, match=msg):
184196
NA | 5
@@ -191,6 +203,12 @@ def test_logical_xor():
191203
assert False ^ NA is NA
192204
assert NA ^ NA is NA
193205

206+
# GH#58427
207+
assert NA ^ np.bool_(True) is NA
208+
assert np.bool_(True) ^ NA is NA
209+
assert NA ^ np.bool_(False) is NA
210+
assert np.bool_(False) ^ NA is NA
211+
194212
msg = "unsupported operand type"
195213
with pytest.raises(TypeError, match=msg):
196214
NA ^ 5

0 commit comments

Comments
 (0)