Skip to content

Commit cf1791e

Browse files
committed
NA in arr should return True if arr contains NA
1 parent 7c3584c commit cf1791e

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

pandas/core/arrays/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def __contains__(self, item) -> bool:
361361
# comparisons of any item to pd.NA always return pd.NA, so e.g. "a" in [pd.NA]
362362
# would raise a TypeError. The implementation below works around that.
363363
if isna(item):
364-
return False
364+
return isna(self).any() if self._can_hold_na else False
365365
else:
366366
return (item == self).any()
367367

pandas/tests/arrays/categorical/test_operators.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,9 @@ def test_contains(self, ordered):
406406
cat = Categorical([np.nan, "a"], ordered=ordered)
407407
assert "a" in cat
408408
assert "x" not in cat
409-
assert pd.NA not in cat
409+
assert pd.NA in cat
410+
411+
cat = cat[::-1]
412+
assert "a" in cat
413+
assert "x" not in cat
414+
assert pd.NA in cat

0 commit comments

Comments
 (0)