Skip to content

Commit 4ed1492

Browse files
Backport PR #35877: REGR: DatetimeIndex.intersection incorrectly raising AssertionError (#35879)
Co-authored-by: jbrockmendel <[email protected]>
1 parent 4ca2bbd commit 4ed1492

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

doc/source/whatsnew/v1.1.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17-
17+
- Regression in :meth:`DatetimeIndex.intersection` incorrectly raising ``AssertionError`` when intersecting against a list (:issue:`35876`)
1818
-
1919
-
2020

pandas/core/indexes/datetimelike.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -700,16 +700,16 @@ def intersection(self, other, sort=False):
700700
if result.freq is None:
701701
# TODO: no tests rely on this; needed?
702702
result = result._with_freq("infer")
703-
assert result.name == res_name
703+
result.name = res_name
704704
return result
705705

706706
elif not self._can_fast_intersect(other):
707707
result = Index.intersection(self, other, sort=sort)
708-
assert result.name == res_name
709708
# We need to invalidate the freq because Index.intersection
710709
# uses _shallow_copy on a view of self._data, which will preserve
711710
# self.freq if we're not careful.
712711
result = result._with_freq(None)._with_freq("infer")
712+
result.name = res_name
713713
return result
714714

715715
# to make our life easier, "sort" the two ranges

pandas/tests/indexes/datetimes/test_setops.py

+7
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,13 @@ def test_intersection_bug(self):
470470
tm.assert_index_equal(result, b)
471471
assert result.freq == b.freq
472472

473+
def test_intersection_list(self):
474+
# GH#35876
475+
values = [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-02-01")]
476+
idx = pd.DatetimeIndex(values, name="a")
477+
res = idx.intersection(values)
478+
tm.assert_index_equal(res, idx)
479+
473480
def test_month_range_union_tz_pytz(self, sort):
474481
from pytz import timezone
475482

0 commit comments

Comments
 (0)