Skip to content

REF: simplify wrapping in apply_index #34555

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
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
26 changes: 9 additions & 17 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ from pandas._libs.tslibs.ccalendar cimport DAY_NANOS, get_days_in_month, dayofwe
from pandas._libs.tslibs.conversion cimport (
convert_datetime_to_tsobject,
localize_pydatetime,
normalize_i8_timestamps,
)
from pandas._libs.tslibs.nattype cimport NPY_NAT, c_NaT as NaT
from pandas._libs.tslibs.np_datetime cimport (
Expand Down Expand Up @@ -79,21 +80,14 @@ cdef bint _is_normalized(datetime dt):
def apply_index_wraps(func):
# Note: normally we would use `@functools.wraps(func)`, but this does
# not play nicely with cython class methods
def wrapper(self, other):

is_index = not util.is_array(other._data)

# operate on DatetimeArray
arr = other._data if is_index else other

result = func(self, arr)
def wrapper(self, other) -> np.ndarray:
# other is a DatetimeArray

if is_index:
# Wrap DatetimeArray result back to DatetimeIndex
result = type(other)._simple_new(result, name=other.name)
result = func(self, other)
result = np.asarray(result)

if self.normalize:
result = result.to_period('D').to_timestamp()
result = normalize_i8_timestamps(result.view("i8"), None)
return result

# do @functools.wraps(func) manually since it doesn't work on cdef funcs
Expand Down Expand Up @@ -1889,7 +1883,7 @@ cdef class YearOffset(SingleConstructorOffset):
shifted = shift_quarters(
dtindex.asi8, self.n, self.month, self._day_opt, modby=12
)
return type(dtindex)._simple_new(shifted, dtype=dtindex.dtype)
return shifted


cdef class BYearEnd(YearOffset):
Expand Down Expand Up @@ -2033,7 +2027,7 @@ cdef class QuarterOffset(SingleConstructorOffset):
shifted = shift_quarters(
dtindex.asi8, self.n, self.startingMonth, self._day_opt
)
return type(dtindex)._simple_new(shifted, dtype=dtindex.dtype)
return shifted


cdef class BQuarterEnd(QuarterOffset):
Expand Down Expand Up @@ -2139,7 +2133,7 @@ cdef class MonthOffset(SingleConstructorOffset):
@apply_index_wraps
def apply_index(self, dtindex):
shifted = shift_months(dtindex.asi8, self.n, self._day_opt)
return type(dtindex)._simple_new(shifted, dtype=dtindex.dtype)
return shifted

cpdef __setstate__(self, state):
state.pop("_use_relativedelta", False)
Expand Down Expand Up @@ -2503,8 +2497,6 @@ cdef class Week(SingleConstructorOffset):
@apply_index_wraps
def apply_index(self, dtindex):
if self.weekday is None:
# integer addition on PeriodIndex is deprecated,
# so we use _time_shift directly
td = timedelta(days=7 * self.n)
td64 = np.timedelta64(td, "ns")
return dtindex + td64
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,9 @@ def _add_offset(self, offset):
values = self.tz_localize(None)
else:
values = self
result = offset.apply_index(values).tz_localize(self.tz)
result = offset.apply_index(values)
result = DatetimeArray._simple_new(result)
result = result.tz_localize(self.tz)

except NotImplementedError:
warnings.warn(
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3524,7 +3524,7 @@ def test_offset_whole_year(self):
with tm.assert_produces_warning(None):
# GH#22535 check that we don't get a FutureWarning from adding
# an integer array to PeriodIndex
result = SemiMonthEnd().apply_index(s)
result = SemiMonthEnd() + s

exp = DatetimeIndex(dates[1:])
tm.assert_index_equal(result, exp)
Expand Down Expand Up @@ -3672,7 +3672,7 @@ def test_apply_index(self, case):
with tm.assert_produces_warning(None):
# GH#22535 check that we don't get a FutureWarning from adding
# an integer array to PeriodIndex
result = offset.apply_index(s)
result = offset + s

exp = DatetimeIndex(cases.values())
tm.assert_index_equal(result, exp)
Expand Down Expand Up @@ -3783,7 +3783,7 @@ def test_offset_whole_year(self):
with tm.assert_produces_warning(None):
# GH#22535 check that we don't get a FutureWarning from adding
# an integer array to PeriodIndex
result = SemiMonthBegin().apply_index(s)
result = SemiMonthBegin() + s

exp = DatetimeIndex(dates[1:])
tm.assert_index_equal(result, exp)
Expand Down Expand Up @@ -3936,7 +3936,7 @@ def test_apply_index(self, case):
with tm.assert_produces_warning(None):
# GH#22535 check that we don't get a FutureWarning from adding
# an integer array to PeriodIndex
result = offset.apply_index(s)
result = offset + s

exp = DatetimeIndex(cases.values())
tm.assert_index_equal(result, exp)
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/tseries/offsets/test_yqm_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ def test_apply_index(cls, n):

res = rng + offset
assert res.freq is None # not retained
res_v2 = offset.apply_index(rng)
assert (res == res_v2).all()
assert res[0] == rng[0] + offset
assert res[-1] == rng[-1] + offset
res2 = ser + offset
Expand Down