Skip to content

BUG: Fix Series.isin with date/time-like dtypes (GH5021) #5303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ Bug Fixes
- Fixed issue with ``drop`` and a non-unique index on Series (:issue:`5248`)
- Fixed seg fault in C parser caused by passing more names than columns in
the file. (:issue:`5156`)
- Fix ``Series.isin`` with date/time-like dtypes (:issue:`5021`)

pandas 0.12.0
-------------
Expand Down
14 changes: 13 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2077,8 +2077,20 @@ def isin(self, values):
raise TypeError("only list-like objects are allowed to be passed"
" to Series.isin(), you passed a "
"{0!r}".format(type(values).__name__))

# may need i8 conversion for proper membership testing
comps = _values_from_object(self)
if com.is_datetime64_dtype(self):
from pandas.tseries.tools import to_datetime
values = Series(to_datetime(values)).values.view('i8')
comps = comps.view('i8')
elif com.is_timedelta64_dtype(self):
from pandas.tseries.timedeltas import to_timedelta
values = Series(to_timedelta(values)).values.view('i8')
comps = comps.view('i8')

value_set = set(values)
result = lib.ismember(_values_from_object(self), value_set)
result = lib.ismember(comps, value_set)
return self._constructor(result, index=self.index).__finalize__(self)

def between(self, left, right, inclusive=True):
Expand Down
34 changes: 33 additions & 1 deletion pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4840,7 +4840,7 @@ def test_isin(self):
assert_series_equal(result, expected)

def test_isin_with_string_scalar(self):
#GH4763
# GH4763
s = Series(['A', 'B', 'C', 'a', 'B', 'B', 'A', 'C'])
with tm.assertRaises(TypeError):
s.isin('a')
Expand All @@ -4849,6 +4849,38 @@ def test_isin_with_string_scalar(self):
s = Series(['aaa', 'b', 'c'])
s.isin('aaa')

def test_isin_with_i8(self):
# GH 5021

expected = Series([True,True,False,False,False])
expected2 = Series([False,True,False,False,False])

# datetime64[ns]
s = Series(date_range('jan-01-2013','jan-05-2013'))

result = s.isin(s[0:2])
assert_series_equal(result, expected)

result = s.isin(s[0:2].values)
assert_series_equal(result, expected)

# fails on dtype conversion in the first place
if not _np_version_under1p7:
result = s.isin(s[0:2].values.astype('datetime64[D]'))
assert_series_equal(result, expected)

result = s.isin([s[1]])
assert_series_equal(result, expected2)

result = s.isin([np.datetime64(s[1])])
assert_series_equal(result, expected2)

# timedelta64[ns]
if not _np_version_under1p7:
s = Series(pd.to_timedelta(lrange(5),unit='d'))
result = s.isin(s[0:2])
assert_series_equal(result, expected)

#------------------------------------------------------------------------------
# TimeSeries-specific
def test_cummethods_bool(self):
Expand Down