Skip to content

Updating and re-opening PR #48129 #50541

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

Closed
wants to merge 16 commits into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ Datetimelike
- Bug in :func:`to_datetime` was throwing ``ValueError`` when parsing dates with ISO8601 format where some values were not zero-padded (:issue:`21422`)
- Bug in :func:`to_datetime` was giving incorrect results when using ``format='%Y%m%d'`` and ``errors='ignore'`` (:issue:`26493`)
- Bug in :func:`to_datetime` was failing to parse date strings ``'today'`` and ``'now'`` if ``format`` was not ISO8601 (:issue:`50359`)
- Bug where adding a :class:`DateOffset` to a :class:`DatetimeIndex` or :class:`Series` over a Daylight Savings Time boundary would produce an incorrect result (:issue:`43784`)
-

Timedelta
Expand Down
11 changes: 11 additions & 0 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@ cdef class Day(Tick):
_td64_unit = "D"
_period_dtype_code = PeriodDtypeCode.D
_creso = NPY_DATETIMEUNIT.NPY_FR_D
_use_relativedelta = True


cdef class Hour(Tick):
Expand Down Expand Up @@ -1439,6 +1440,7 @@ cdef class BusinessMixin(SingleConstructorOffset):
"""
Mixin to business types to provide related functions.
"""
_use_relativedelta = True

cdef readonly:
timedelta _offset
Expand Down Expand Up @@ -2062,6 +2064,7 @@ cdef class WeekOfMonthMixin(SingleConstructorOffset):
"""
Mixin for methods common to WeekOfMonth and LastWeekOfMonth.
"""
_use_relativedelta = True

cdef readonly:
int weekday, week
Expand Down Expand Up @@ -2106,6 +2109,7 @@ cdef class YearOffset(SingleConstructorOffset):
DateOffset that just needs a month.
"""
_attributes = tuple(["n", "normalize", "month"])
_use_relativedelta = True

# FIXME(cython#4446): python annotation here gives compile-time errors
# _default_month: int
Expand Down Expand Up @@ -2271,6 +2275,7 @@ cdef class QuarterOffset(SingleConstructorOffset):
# FIXME(cython#4446): python annotation here gives compile-time errors
# _default_starting_month: int
# _from_name_starting_month: int
_use_relativedelta = True

cdef readonly:
int startingMonth
Expand Down Expand Up @@ -2447,6 +2452,8 @@ cdef class QuarterBegin(QuarterOffset):
# Month-Based Offset Classes

cdef class MonthOffset(SingleConstructorOffset):
_use_relativedelta = True

def is_on_offset(self, dt: datetime) -> bool:
if self.normalize and not _is_normalized(dt):
return False
Expand Down Expand Up @@ -2573,6 +2580,7 @@ cdef class SemiMonthOffset(SingleConstructorOffset):
_default_day_of_month = 15
_min_day_of_month = 2
_attributes = tuple(["n", "normalize", "day_of_month"])
_use_relativedelta = True

cdef readonly:
int day_of_month
Expand Down Expand Up @@ -2790,6 +2798,7 @@ cdef class Week(SingleConstructorOffset):
_inc = timedelta(weeks=1)
_prefix = "W"
_attributes = tuple(["n", "normalize", "weekday"])
_use_relativedelta = True

cdef readonly:
object weekday # int or None
Expand Down Expand Up @@ -3069,6 +3078,7 @@ cdef class LastWeekOfMonth(WeekOfMonthMixin):
# Special Offset Classes

cdef class FY5253Mixin(SingleConstructorOffset):
_use_relativedelta = True
cdef readonly:
int startingMonth
int weekday
Expand Down Expand Up @@ -3540,6 +3550,7 @@ cdef class Easter(SingleConstructorOffset):
>>> ts + pd.offsets.Easter()
Timestamp('2022-04-17 00:00:00')
"""
_use_relativedelta = True

cpdef __setstate__(self, state):
self.n = state.pop("n")
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,10 @@ def _add_offset(self, offset) -> DatetimeArray:
assert not isinstance(offset, Tick)

if self.tz is not None:
values = self.tz_localize(None)
if not offset._use_relativedelta:
Copy link
Member

Choose a reason for hiding this comment

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

is there a viable way to handle this inside the Offset code? in theory the DTA code shouldn't need to know anything about _use_relativedelta

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is with how these dates are localized, and I don't believe there is a good way to modify this in the Offset code.

values = self.tz_convert("utc").tz_localize(None)
else:
values = self.tz_localize(None)
else:
values = self

Expand All @@ -782,7 +785,10 @@ def _add_offset(self, offset) -> DatetimeArray:
else:
result = DatetimeArray._simple_new(result, dtype=result.dtype)
if self.tz is not None:
result = result.tz_localize(self.tz)
if not offset._use_relativedelta:
result = result.tz_localize("utc").tz_convert(self.tz)
else:
result = result.tz_localize(self.tz)

return result

Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/tseries/offsets/test_dst.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
YearEnd,
)

import pandas as pd
import pandas._testing as tm
from pandas.util.version import Version

# error: Module has no attribute "__version__"
Expand Down Expand Up @@ -233,3 +235,30 @@ def test_nontick_offset_with_ambiguous_time_error(original_dt, target_dt, offset
msg = f"Cannot infer dst time from {target_dt}, try using the 'ambiguous' argument"
with pytest.raises(pytz.AmbiguousTimeError, match=msg):
localized_dt + offset


def test_series_dst_addition():
# GH#43784
startdates = pd.Series(
[
Timestamp("2020-10-25", tz="Europe/Berlin"),
Timestamp("2017-03-12", tz="US/Pacific"),
]
)
offset1 = DateOffset(hours=3)
offset2 = DateOffset(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.

looks like this only tests the DateOffset class, for which _use_relativedelta can be True. but the code you changed affects a bunch of other classes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I'll try and work on some tests for the other classes.


expected1 = pd.Series(
[Timestamp("2020-10-25 02:00:00+01:00"), Timestamp("2017-03-12 04:00:00-07:00")]
)

expected2 = pd.Series(
[Timestamp("2020-10-26 00:00:00+01:00"), Timestamp("2017-03-13 00:00:00-07:00")]
)

result1 = startdates + offset1
result2 = startdates + offset2

tm.assert_series_equal(result1, expected1)

tm.assert_series_equal(result2, expected2)