Skip to content

Commit 5c0ecc1

Browse files
committed
adding yet another testcase and fixing pep8-problems
1 parent 0aa9b53 commit 5c0ecc1

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

pandas/tests/test_algos.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,16 +615,37 @@ def test_categorical_from_codes(self):
615615
result = algos.isin(Sd, St)
616616
tm.assert_numpy_array_equal(expected, result)
617617

618-
def test_same_object_is_in(self):
618+
def test_same_nan_is_in(self):
619619
# GH 22160
620620
# 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
622624
comps = [np.nan] # could be casted to float64
623625
values = [np.nan]
624626
expected = np.array([True])
625627
result = algos.isin(comps, values)
626628
tm.assert_numpy_array_equal(expected, result)
627629

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+
628649
def test_different_nans(self):
629650
# GH 22160
630651
# the current behavior is:
@@ -634,7 +655,7 @@ def test_different_nans(self):
634655
#
635656
# this test case only ensures it doesn't happen accidentally
636657
#
637-
comps = [float('nan')]
658+
comps = [float('nan')]
638659
values = [float('nan')]
639660
assert comps[0] is not values[0] # different nan-objects
640661

@@ -643,11 +664,13 @@ def test_different_nans(self):
643664
tm.assert_numpy_array_equal(np.array([False]), result)
644665

645666
# 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))
647669
tm.assert_numpy_array_equal(np.array([False]), result)
648670

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))
651674
tm.assert_numpy_array_equal(np.array([True]), result)
652675

653676
def test_no_cast(self):

0 commit comments

Comments
 (0)