Skip to content
Open
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
7 changes: 7 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clearer just to write diff != Timedelta(days=1)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of never mind, didn't see the %

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think this condition ensures they share a point in common. e.g. what if both freqs are Week and they are offset by one day?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the week freqs are created with CDay(7), then self.freq.n is equal to 7 and _can_fast_intersect returns false on the final check:

return self.freq.n == 1

If the freqs are "W" then it seems they snap to Sunday regardless of starting offset.

For specific "W" freqs (e.g. "W-MON", "W-TUE") it returns false on this check:

elif other.freq != self.freq:
    return False

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the week freqs are created with CDay(7)

Why would you assume that. I was thinking pd.offsets.Week(1)

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
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Loading