Skip to content

BUG: DTI.intersection with DST transition #48167

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 7 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.loc` not aligning index in some cases when setting a :class:`DataFrame` (:issue:`47578`)
- Fixed regression in :meth:`DataFrame.loc` setting a length-1 array like value to a single value in the DataFrame (:issue:`46268`)
- Fixed regression in setting ``None`` or non-string value into a ``string``-dtype Series using a mask (:issue:`47628`)
- Fixed regression in :meth:`DatetimeIndex.intersection` when the :class:`DatetimeIndex` has dates crossing daylight savings time (:issue:`46702`)
- Fixed regression in :func:`merge` throwing an error when passing a :class:`Series` with a multi-level name (:issue:`47946`)
- Fixed regression in :meth:`DataFrame.eval` creating a copy when updating inplace (:issue:`47449`)
-
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ def _formatter_func(self):
# --------------------------------------------------------------------
# Set Operation Methods

def _can_range_setop(self, other) -> bool:
# GH 46702: If self or other have non-UTC tzs, DST transitions prevent
# range representation due to no singular step
if self.tz is not None and not timezones.is_utc(self.tz):
return False
other_tz = getattr(other, "tz", None)
if other_tz is not None and not timezones.is_utc(other_tz):
Copy link
Member

Choose a reason for hiding this comment

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

fine if you want to do quick fix, but could also include fixed-offset tzs. also i think we're assured matching types/dtypes at this point

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a check for not timezones.is_fixed_offset.

Yeah I think a more complete check would be to add a .fold attribute and then check self.fold.nunique > 1. That would allow some DatetimeIndexes to still use the distpatch to range ops. #47130

return False
return super()._can_range_setop(other)

def union_many(self, others):
"""
A bit of a hack to accelerate unioning a collection of indexes.
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,12 @@ def test_intersection_bug(self):
result = a.intersection(b)
tm.assert_index_equal(result, b)
assert result.freq == b.freq

@pytest.mark.parametrize("tz", [None, "UTC", "Europe/Berlin"])
def test_intersection_dst_transition(self, tz):
# GH 46702: Europe/Berlin has DST transition
idx1 = date_range("2020-03-27", periods=5, freq="D", tz=tz)
idx2 = date_range("2020-03-30", periods=5, freq="D", tz=tz)
result = idx1.intersection(idx2)
expected = date_range("2020-03-30", periods=2, freq="D", tz=tz)
tm.assert_index_equal(result, expected)