Skip to content

REF: RelativeDeltaOffset.apply_index operate on ndarray #34613

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 1 commit into from
Jun 8, 2020
Merged
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
19 changes: 10 additions & 9 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ cdef class BaseOffset:
# ------------------------------------------------------------------

@apply_index_wraps
def apply_index(self, index):
def apply_index(self, dtindex):
"""
Vectorized apply of DateOffset to DatetimeIndex,
raises NotImplementedError for offsets without a
Expand Down Expand Up @@ -1028,7 +1028,7 @@ cdef class RelativeDeltaOffset(BaseOffset):
return other + timedelta(self.n)

@apply_index_wraps
def apply_index(self, index):
def apply_index(self, dtindex):
"""
Vectorized apply of DateOffset to DatetimeIndex,
raises NotImplementedError for offsets without a
Expand All @@ -1040,8 +1040,9 @@ cdef class RelativeDeltaOffset(BaseOffset):

Returns
-------
DatetimeIndex
ndarray[datetime64[ns]]
"""
dt64other = np.asarray(dtindex)
kwds = self.kwds
relativedelta_fast = {
"years",
Expand All @@ -1058,12 +1059,12 @@ cdef class RelativeDeltaOffset(BaseOffset):

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

weeks = kwds.get("weeks", 0) * self.n
if weeks:
index = index + timedelta(days=7 * weeks)
dt64other = dt64other + Timedelta(days=7 * weeks)

timedelta_kwds = {
k: v
Expand All @@ -1072,11 +1073,11 @@ cdef class RelativeDeltaOffset(BaseOffset):
}
if timedelta_kwds:
delta = Timedelta(**timedelta_kwds)
index = index + (self.n * delta)
return index
dt64other = dt64other + (self.n * delta)
return dt64other
elif not self._use_relativedelta and hasattr(self, "_offset"):
# timedelta
return index + (self._offset * self.n)
return dt64other + Timedelta(self._offset * self.n)
else:
# relativedelta with other keywords
kwd = set(kwds) - relativedelta_fast
Expand Down