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