Skip to content

Commit fdb9deb

Browse files
committed
minor changes
1 parent 8a24f0d commit fdb9deb

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

pandas/core/arrays/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def __contains__(self, item) -> bool:
362362
# comparisons of any item to pd.NA always return pd.NA, so e.g. "a" in [pd.NA]
363363
# would raise a TypeError. The implementation below works around that.
364364
if item is self.dtype.na_value:
365-
return isna(self).any() if self._can_hold_na else False
365+
return self.isna().any() if self._can_hold_na else False
366366
elif is_scalar(item) and isna(item):
367367
return False
368368
else:

pandas/tests/extension/base/interface.py

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,30 @@ def test_can_hold_na_valid(self, data):
2929
# GH-20761
3030
assert data._can_hold_na is True
3131

32-
def test_contains(self, data):
32+
def test_contains(self, data, data_missing):
3333
# GH-37867
34+
# Tests for membership checks. Membership checks for nan-likes is tricky and
35+
# the settled on rule is: `nan_like in arr` is True if nan_like is
36+
# arr.dtype.na_value and arr.isna().any() is True. Else the check returns False.
3437

35-
data = data[~data.isna()]
38+
for this_data in [data, data_missing]:
39+
scalar = this_data[~this_data.isna()][0]
3640

37-
scalar = data[0]
41+
assert scalar in this_data
42+
assert "124jhujbhjhb5" not in data
3843

39-
assert scalar in data
40-
assert "124jhujbhjhb5" not in data
44+
na_value = this_data.dtype.na_value
4145

42-
na_value = data.dtype.na_value
46+
if this_data.isna().any():
47+
assert na_value in this_data
48+
else:
49+
assert na_value not in this_data
4350

44-
assert na_value not in data
45-
46-
for na_value_type in {None, np.nan, pd.NA, pd.NaT}:
47-
assert na_value_type not in data
48-
49-
def test_contains_nan(self, data_missing):
50-
# GH-37867
51-
data = data_missing
52-
53-
scalar = data[~data.isna()][0]
54-
55-
assert scalar in data
56-
57-
na_value = data.dtype.na_value
58-
59-
if data.isna().any():
60-
assert na_value in data
61-
else:
62-
assert na_value not in data
63-
64-
for na_value_type in {None, np.nan, pd.NA, pd.NaT}:
65-
if na_value_type is na_value:
66-
continue
67-
assert na_value_type not in data
51+
# this_data can never contain other nan-likes than na_value
52+
for na_value_type in {None, np.nan, pd.NA, pd.NaT}:
53+
if na_value_type is na_value:
54+
continue
55+
assert na_value_type not in this_data
6856

6957
def test_memory_usage(self, data):
7058
s = pd.Series(data)

0 commit comments

Comments
 (0)