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 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
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
31 changes: 27 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,33 @@ 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)

dta = ser[:2]._values.astype("M8[s]")
result = ser.isin(dta)
tm.assert_series_equal(result, expected)

@pytest.mark.xfail(
reason="DTA._from_sequence incorrectly treats Timestamp[s].value as "
"nanoseconds."
)
def test_isin_datetimelike_mismatched_reso_list(self):
expected = Series([True, True, False, False, False])

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

dta = ser[:2]._values.astype("M8[s]")
result = ser.isin(list(dta))
tm.assert_series_equal(result, expected)

def test_isin_with_i8(self):
# GH#5021

Expand All @@ -58,10 +85,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