Skip to content

BUG: Fixed inconsistent offset behavior for series #43784 #48129

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 15 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/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Categorical
Datetimelike
^^^^^^^^^^^^
- Bug in :func:`pandas.infer_freq`, raising ``TypeError`` when inferred on :class:`RangeIndex` (:issue:`47084`)
- 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 @@ -1148,6 +1148,7 @@ cdef class Day(Tick):
_td64_unit = "D"
_period_dtype_code = PeriodDtypeCode.D
_reso = NPY_DATETIMEUNIT.NPY_FR_D
_use_relativedelta = True


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

cdef readonly:
timedelta _offset
Expand Down Expand Up @@ -2068,6 +2070,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 @@ -2112,6 +2115,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 @@ -2277,6 +2281,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 @@ -2448,6 +2453,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 @@ -2548,6 +2555,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 @@ -2750,6 +2758,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 @@ -3027,6 +3036,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 @@ -3496,6 +3506,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 @@ -694,7 +694,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:
values = self.tz_convert("utc").tz_localize(None)
else:
values = self.tz_localize(None)
else:
values = self

Expand All @@ -716,7 +719,10 @@ def _add_offset(self, offset) -> DatetimeArray:
result = DatetimeArray._simple_new(result, dtype=result.dtype)
if self.tz is not None:
# FIXME: tz_localize with non-nano
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.tests.tseries.offsets.test_offsets import get_utc_offset_hours
from pandas.util.version import Version

Expand Down Expand Up @@ -228,3 +230,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)

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)