Skip to content

Commit 3bd9d15

Browse files
committed
BUG: fix DatetimeIndex.isin close #1763
1 parent 4083689 commit 3bd9d15

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pandas 0.8.2
9393
- Basic indexing now works on MultiIndex with > 1000000 elements, regression
9494
from earlier version of pandas (#1757)
9595
- Handle non-float64 dtypes in fast DataFrame.corr/cov code paths (#1761)
96+
- Fix DatetimeIndex.isin to function properly (#1763)
9697

9798
pandas 0.8.1
9899
============

pandas/tseries/index.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,28 @@ def __contains__(self, key):
499499
except (KeyError, TypeError):
500500
return False
501501

502+
def isin(self, values):
503+
"""
504+
Compute boolean array of whether each index value is found in the
505+
passed set of values
506+
507+
Parameters
508+
----------
509+
values : set or sequence of values
510+
511+
Returns
512+
-------
513+
is_contained : ndarray (boolean dtype)
514+
"""
515+
if not isinstance(values, DatetimeIndex):
516+
try:
517+
values = DatetimeIndex(values)
518+
except ValueError:
519+
return self.asobject.isin(values)
520+
521+
value_set = set(values.asi8)
522+
return lib.ismember(self.asi8, value_set)
523+
502524
def to_datetime(self, dayfirst=False):
503525
return self.copy()
504526

pandas/tseries/tests/test_timeseries.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,17 @@ def test_append_numpy_bug_1681(self):
13041304
result = a.append(c)
13051305
self.assert_((result['B'] == dr).all())
13061306

1307+
def test_isin(self):
1308+
index = tm.makeDateIndex(4)
1309+
result = index.isin(index)
1310+
self.assert_(result.all())
1311+
1312+
result = index.isin(list(index))
1313+
self.assert_(result.all())
1314+
1315+
assert_almost_equal(index.isin([index[2], 5]),
1316+
[False, False, True, False])
1317+
13071318
class TestLegacySupport(unittest.TestCase):
13081319

13091320
@classmethod

0 commit comments

Comments
 (0)