diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 7e6461f0fab5e..08588b8795668 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -637,6 +637,13 @@ def _can_fast_intersect(self, other: Self) -> bool: # Because freq is not None, we must then be monotonic decreasing return False + # for non-anchored frequencies, need to check that the two + # indexes actually share a common point + # GH#44025 + diff = other[0] - self[0] + if diff != Timedelta(0) and diff.total_seconds() % 86400 != 0: + return False + # this along with matching freqs ensure that we "line up", # so intersection will preserve freq # Note we are assuming away Ticks, as those go through _range_intersect diff --git a/pandas/tests/indexes/datetimes/test_setops.py b/pandas/tests/indexes/datetimes/test_setops.py index 7a68cb867c94e..230fd67bc9b82 100644 --- a/pandas/tests/indexes/datetimes/test_setops.py +++ b/pandas/tests/indexes/datetimes/test_setops.py @@ -7,6 +7,7 @@ import numpy as np import pytest +from pandas._libs.tslibs.timedeltas import Timedelta import pandas.util._test_decorators as td import pandas as pd @@ -757,3 +758,16 @@ def test_intersection_non_nano_rangelike(): freq="D", ) tm.assert_index_equal(result, expected) + + +def test_cday_intersection_empty(): + # GH#44025 + off = pd.offsets.CDay(1, normalize=False) + ts = Timestamp("2021-10-13 09:00") + ts2 = ts + Timedelta("1 hour") + + dti1 = date_range(start=ts, periods=10, freq=off) + dti2 = date_range(start=ts2, periods=10, freq=off) + result = dti1.intersection(dti2) + expected = DatetimeIndex([], dtype="datetime64[ns]") + tm.assert_index_equal(result, expected)