Skip to content

Commit 74b08ac

Browse files
committed
REGR: DatetimeIndex.intersection pandas-dev#42104
1 parent 1ff6970 commit 74b08ac

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pandas/core/indexes/datetimelike.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,8 @@ def _can_fast_intersect(self: _T, other: _T) -> bool:
693693
elif self.freq.is_anchored():
694694
# this along with matching freqs ensure that we "line up",
695695
# so intersection will preserve freq
696-
return True
696+
# GH#42104
697+
return self.freq.n == 1
697698

698699
elif isinstance(self.freq, Tick):
699700
# We "line up" if and only if the difference between two of our points
@@ -702,7 +703,8 @@ def _can_fast_intersect(self: _T, other: _T) -> bool:
702703
remainder = diff % self.freq.delta
703704
return remainder == Timedelta(0)
704705

705-
return True
706+
# GH#42104
707+
return self.freq.n == 1
706708

707709
def _can_fast_union(self: _T, other: _T) -> bool:
708710
# Assumes that type(self) == type(other), as per the annotation

pandas/tests/indexes/datetimes/test_setops.py

+17
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,23 @@ def test_setops_preserve_freq(self, tz):
391391
assert result.freq == rng.freq
392392
assert result.tz == rng.tz
393393

394+
def test_intersection_non_tick_no_fastpath(self):
395+
# GH#42104
396+
dti = DatetimeIndex(
397+
[
398+
"2018-12-31",
399+
"2019-03-31",
400+
"2019-06-30",
401+
"2019-09-30",
402+
"2019-12-31",
403+
"2020-03-31",
404+
],
405+
freq="Q-DEC",
406+
)
407+
result = dti[::2].intersection(dti[1::2])
408+
expected = dti[:0]
409+
tm.assert_index_equal(result, expected)
410+
394411

395412
class TestBusinessDatetimeIndex:
396413
def setup_method(self, method):

0 commit comments

Comments
 (0)