Skip to content

REGR: DatetimeIndex.intersection incorrectly raising AssertionError #35877

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 12 commits into from
Aug 24, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~

- Regression in :meth:`DatetimeIndex.intersection` incorrectly raising ``AssertionError`` when intersecting against a list (:issue:`35876`)
-
-

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,16 +704,16 @@ def intersection(self, other, sort=False):
if result.freq is None:
# TODO: no tests rely on this; needed?
result = result._with_freq("infer")
assert result.name == res_name
result.name = res_name
return result

elif not self._can_fast_intersect(other):
result = Index.intersection(self, other, sort=sort)
assert result.name == res_name
# We need to invalidate the freq because Index.intersection
# uses _shallow_copy on a view of self._data, which will preserve
# self.freq if we're not careful.
result = result._with_freq(None)._with_freq("infer")
result.name = res_name
return result

# to make our life easier, "sort" the two ranges
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@ def test_intersection_bug(self):
tm.assert_index_equal(result, b)
assert result.freq == b.freq

def test_intersection_list(self):
# GH#35876
values = [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-02-01")]
idx = pd.DatetimeIndex(values, name="a")
res = idx.intersection(values)
tm.assert_index_equal(res, idx)

def test_month_range_union_tz_pytz(self, sort):
from pytz import timezone

Expand Down