Skip to content

BUG: NA.__and__, __or__, __xor__ with np.bool_ objects #61768

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 1 commit into from
Jul 3, 2025
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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ Indexing
Missing
^^^^^^^
- Bug in :meth:`DataFrame.fillna` and :meth:`Series.fillna` that would ignore the ``limit`` argument on :class:`.ExtensionArray` dtypes (:issue:`58001`)
- Bug in :meth:`NA.__and__`, :meth:`NA.__or__` and :meth:`NA.__xor__` when operating with ``np.bool_`` objects (:issue:`58427`)
-

MultiIndex
Expand Down
10 changes: 9 additions & 1 deletion pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ class NAType(C_NAType):
return False
elif other is True or other is C_NA:
return NA
elif util.is_bool_object(other):
if not other:
return False
return NA
return NotImplemented

__rand__ = __and__
Expand All @@ -480,12 +484,16 @@ class NAType(C_NAType):
return True
elif other is False or other is C_NA:
return NA
elif util.is_bool_object(other):
if not other:
return NA
return True
return NotImplemented

__ror__ = __or__

def __xor__(self, other):
if other is False or other is True or other is C_NA:
if util.is_bool_object(other) or other is C_NA:
return NA
return NotImplemented

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/scalar/test_na_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ def test_logical_and():
assert False & NA is False
assert NA & NA is NA

# GH#58427
assert NA & np.bool_(True) is NA
assert np.bool_(True) & NA is NA
assert NA & np.bool_(False) is False
assert np.bool_(False) & NA is False

msg = "unsupported operand type"
with pytest.raises(TypeError, match=msg):
NA & 5
Expand All @@ -179,6 +185,12 @@ def test_logical_or():
assert False | NA is NA
assert NA | NA is NA

# GH#58427
assert NA | np.bool_(True) is True
assert np.bool_(True) | NA is True
assert NA | np.bool_(False) is NA
assert np.bool_(False) | NA is NA

msg = "unsupported operand type"
with pytest.raises(TypeError, match=msg):
NA | 5
Expand All @@ -191,6 +203,12 @@ def test_logical_xor():
assert False ^ NA is NA
assert NA ^ NA is NA

# GH#58427
assert NA ^ np.bool_(True) is NA
assert np.bool_(True) ^ NA is NA
assert NA ^ np.bool_(False) is NA
assert np.bool_(False) ^ NA is NA

msg = "unsupported operand type"
with pytest.raises(TypeError, match=msg):
NA ^ 5
Expand Down
Loading