Skip to content

Commit ff75954

Browse files
authored
REF: RelativeDeltaOffset.apply_index operate on ndarray (#34613)
1 parent 085d0dc commit ff75954

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

pandas/_libs/tslibs/offsets.pyx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ cdef class BaseOffset:
557557
# ------------------------------------------------------------------
558558

559559
@apply_index_wraps
560-
def apply_index(self, index):
560+
def apply_index(self, dtindex):
561561
"""
562562
Vectorized apply of DateOffset to DatetimeIndex,
563563
raises NotImplementedError for offsets without a
@@ -1029,7 +1029,7 @@ cdef class RelativeDeltaOffset(BaseOffset):
10291029
return other + timedelta(self.n)
10301030

10311031
@apply_index_wraps
1032-
def apply_index(self, index):
1032+
def apply_index(self, dtindex):
10331033
"""
10341034
Vectorized apply of DateOffset to DatetimeIndex,
10351035
raises NotImplementedError for offsets without a
@@ -1041,8 +1041,9 @@ cdef class RelativeDeltaOffset(BaseOffset):
10411041
10421042
Returns
10431043
-------
1044-
DatetimeIndex
1044+
ndarray[datetime64[ns]]
10451045
"""
1046+
dt64other = np.asarray(dtindex)
10461047
kwds = self.kwds
10471048
relativedelta_fast = {
10481049
"years",
@@ -1059,12 +1060,12 @@ cdef class RelativeDeltaOffset(BaseOffset):
10591060

10601061
months = (kwds.get("years", 0) * 12 + kwds.get("months", 0)) * self.n
10611062
if months:
1062-
shifted = shift_months(index.asi8, months)
1063-
index = type(index)(shifted, dtype=index.dtype)
1063+
shifted = shift_months(dt64other.view("i8"), months)
1064+
dt64other = shifted.view("datetime64[ns]")
10641065

10651066
weeks = kwds.get("weeks", 0) * self.n
10661067
if weeks:
1067-
index = index + timedelta(days=7 * weeks)
1068+
dt64other = dt64other + Timedelta(days=7 * weeks)
10681069

10691070
timedelta_kwds = {
10701071
k: v
@@ -1073,11 +1074,11 @@ cdef class RelativeDeltaOffset(BaseOffset):
10731074
}
10741075
if timedelta_kwds:
10751076
delta = Timedelta(**timedelta_kwds)
1076-
index = index + (self.n * delta)
1077-
return index
1077+
dt64other = dt64other + (self.n * delta)
1078+
return dt64other
10781079
elif not self._use_relativedelta and hasattr(self, "_offset"):
10791080
# timedelta
1080-
return index + (self._offset * self.n)
1081+
return dt64other + Timedelta(self._offset * self.n)
10811082
else:
10821083
# relativedelta with other keywords
10831084
kwd = set(kwds) - relativedelta_fast

0 commit comments

Comments
 (0)