@@ -615,16 +615,37 @@ def test_categorical_from_codes(self):
615
615
result = algos .isin (Sd , St )
616
616
tm .assert_numpy_array_equal (expected , result )
617
617
618
- def test_same_object_is_in (self ):
618
+ def test_same_nan_is_in (self ):
619
619
# GH 22160
620
620
# nan is special, because from " a is b" doesn't follow "a == b"
621
- # casting to -> np.float64 -> float-object will results in another nan-object
621
+ # at least, isin() should follow python's "np.nan in [nan] == True"
622
+ # casting to -> np.float64 -> another float-object somewher on
623
+ # the way could lead jepardize this behavior
622
624
comps = [np .nan ] # could be casted to float64
623
625
values = [np .nan ]
624
626
expected = np .array ([True ])
625
627
result = algos .isin (comps , values )
626
628
tm .assert_numpy_array_equal (expected , result )
627
629
630
+ def test_same_object_is_in (self ):
631
+ # GH 22160
632
+ # there could be special treatment for nans
633
+ # the user however could define a custom class
634
+ # with similar behavior, then we at least should
635
+ # fall back to usual python's behavior: "a in [a] == True"
636
+ class LikeNan (object ):
637
+ def __eq__ (self ):
638
+ return False
639
+
640
+ def __hash__ (self ):
641
+ return 0
642
+
643
+ a , b = LikeNan (), LikeNan ()
644
+ # same object -> True
645
+ tm .assert_numpy_array_equal (algos .isin ([a ], [a ]), np .array ([True ]))
646
+ # different objects -> False
647
+ tm .assert_numpy_array_equal (algos .isin ([a ], [b ]), np .array ([False ]))
648
+
628
649
def test_different_nans (self ):
629
650
# GH 22160
630
651
# the current behavior is:
@@ -634,7 +655,7 @@ def test_different_nans(self):
634
655
#
635
656
# this test case only ensures it doesn't happen accidentally
636
657
#
637
- comps = [float ('nan' )]
658
+ comps = [float ('nan' )]
638
659
values = [float ('nan' )]
639
660
assert comps [0 ] is not values [0 ] # different nan-objects
640
661
@@ -643,11 +664,13 @@ def test_different_nans(self):
643
664
tm .assert_numpy_array_equal (np .array ([False ]), result )
644
665
645
666
# as object-array:
646
- result = algos .isin (np .asarray (comps , dtype = np .object ), np .asarray (values , dtype = np .object ))
667
+ result = algos .isin (np .asarray (comps , dtype = np .object ),
668
+ np .asarray (values , dtype = np .object ))
647
669
tm .assert_numpy_array_equal (np .array ([False ]), result )
648
670
649
- #as float64-array:
650
- result = algos .isin (np .asarray (comps , dtype = np .float64 ), np .asarray (values , dtype = np .float64 ))
671
+ # as float64-array:
672
+ result = algos .isin (np .asarray (comps , dtype = np .float64 ),
673
+ np .asarray (values , dtype = np .float64 ))
651
674
tm .assert_numpy_array_equal (np .array ([True ]), result )
652
675
653
676
def test_no_cast (self ):
0 commit comments