Skip to content

Commit dd94526

Browse files
committed
adding yet another testcase and fixing pep8-problems
1 parent b6f0512 commit dd94526

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
@@ -614,16 +614,37 @@ def test_categorical_from_codes(self):
614614
result = algos.isin(Sd, St)
615615
tm.assert_numpy_array_equal(expected, result)
616616

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

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+
627648
def test_different_nans(self):
628649
# GH 22160
629650
# the current behavior is:
@@ -633,7 +654,7 @@ def test_different_nans(self):
633654
#
634655
# this test case only ensures it doesn't happen accidentally
635656
#
636-
comps = [float('nan')]
657+
comps = [float('nan')]
637658
values = [float('nan')]
638659
assert comps[0] is not values[0] # different nan-objects
639660

@@ -642,11 +663,13 @@ def test_different_nans(self):
642663
tm.assert_numpy_array_equal(np.array([False]), result)
643664

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

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

652675
def test_no_cast(self):

0 commit comments

Comments
 (0)