Skip to content

BUG: DTA.isin with mismatched resos #49050

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 3 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,10 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
except ValueError:
return isin(self.astype(object), values)

if self.dtype.kind in ["m", "M"]:
self = cast("DatetimeArray | TimedeltaArray", self)
values = values._as_unit(self._unit)

try:
self._check_compatible_with(values)
except (TypeError, ValueError):
Expand Down
24 changes: 20 additions & 4 deletions pandas/tests/series/methods/test_isin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ def test_isin_with_string_scalar(self):
with pytest.raises(TypeError, match=msg):
s.isin("aaa")

def test_isin_datetimelike_mismatched_reso(self):
expected = Series([True, True, False, False, False])

ser = Series(date_range("jan-01-2013", "jan-05-2013"))

# fails on dtype conversion in the first place
day_values = np.asarray(ser[0:2].values).astype("datetime64[D]")
result = ser.isin(day_values)
tm.assert_series_equal(result, expected)

s_values = day_values.astype("M8[s]")
dta = type(ser._values)._simple_new(s_values, dtype=s_values.dtype)
result = ser.isin(dta)
tm.assert_series_equal(result, expected)

# FIXME(2.0): DTA._from_sequence incorrectly treats Timestamp[s].value
# as nanoseconds.
# result = ser.isin(list(dta))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xfail this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, will update

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated + green

# tm.assert_series_equal(result, expected)

def test_isin_with_i8(self):
# GH#5021

Expand All @@ -58,10 +78,6 @@ def test_isin_with_i8(self):
result = s.isin(s[0:2].values)
tm.assert_series_equal(result, expected)

# fails on dtype conversion in the first place
result = s.isin(np.asarray(s[0:2].values).astype("datetime64[D]"))
tm.assert_series_equal(result, expected)

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

Expand Down