File tree 4 files changed +28
-3
lines changed
4 files changed +28
-3
lines changed Original file line number Diff line number Diff line change 37
37
is_array_like ,
38
38
is_dtype_equal ,
39
39
is_list_like ,
40
+ is_scalar ,
40
41
pandas_dtype ,
41
42
)
42
43
from pandas .core .dtypes .dtypes import ExtensionDtype
@@ -360,8 +361,10 @@ def __contains__(self, item) -> bool:
360
361
"""
361
362
# comparisons of any item to pd.NA always return pd.NA, so e.g. "a" in [pd.NA]
362
363
# would raise a TypeError. The implementation below works around that.
363
- if is_valid_nat_for_dtype ( item , self .dtype ) :
364
+ if item is self .dtype . na_value :
364
365
return isna (self ).any () if self ._can_hold_na else False
366
+ elif is_scalar (item ) and isna (item ):
367
+ return False
365
368
else :
366
369
return (item == self ).any ()
367
370
Original file line number Diff line number Diff line change @@ -527,7 +527,7 @@ def test_to_numpy_na_value(dtype, nulls_fixture):
527
527
528
528
529
529
def test_contains ():
530
- # GH-xxxxx
530
+ # GH-37867
531
531
arr = pd .array (["a" , "b" ], dtype = "string" )
532
532
533
533
assert "a" in arr
@@ -538,5 +538,5 @@ def test_contains():
538
538
arr = pd .array (["a" , pd .NA ], dtype = "string" )
539
539
assert "a" in arr
540
540
assert "x" not in arr
541
- assert np .nan in arr
541
+ assert np .nan not in arr
542
542
assert pd .NA in arr
Original file line number Diff line number Diff line change @@ -29,6 +29,23 @@ def test_can_hold_na_valid(self, data):
29
29
# GH-20761
30
30
assert data ._can_hold_na is True
31
31
32
+ def test_contains (self , data ):
33
+ # GH-37867
34
+ scalar = data [~ data .isna ()][0 ]
35
+
36
+ assert scalar in data
37
+
38
+ na_value = data .dtype .na_value
39
+ other_na_value_types = {np .nan , pd .NA , pd .NaT }.difference ({na_value })
40
+ if data .isna ().any ():
41
+ assert na_value in data
42
+ for na_value_type in other_na_value_types :
43
+ assert na_value_type not in data
44
+ else :
45
+ assert na_value not in data
46
+ for na_value_type in other_na_value_types :
47
+ assert na_value_type not in data
48
+
32
49
def test_memory_usage (self , data ):
33
50
s = pd .Series (data )
34
51
result = s .memory_usage (index = False )
Original file line number Diff line number Diff line change @@ -143,6 +143,11 @@ def test_custom_asserts(self):
143
143
with pytest .raises (AssertionError , match = msg ):
144
144
self .assert_frame_equal (a .to_frame (), b .to_frame ())
145
145
146
+ @pytest .mark .xfail (reason = "comparison method not implemented on JSONArray" )
147
+ def test_contains (self , data ):
148
+ # GH-37867
149
+ super ().test_contains (data )
150
+
146
151
147
152
class TestConstructors (BaseJSON , base .BaseConstructorsTests ):
148
153
@pytest .mark .skip (reason = "not implemented constructor from dtype" )
You can’t perform that action at this time.
0 commit comments