Skip to content

Commit 639e3db

Browse files
authored
REF: avoid use of to_perioddelta (#34539)
1 parent 48e03f3 commit 639e3db

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

pandas/_libs/tslibs/offsets.pyx

+12-13
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ from pandas._libs.tslibs.base cimport ABCTimestamp
3636
from pandas._libs.tslibs.ccalendar import (
3737
MONTH_ALIASES, MONTH_TO_CAL_NUM, weekday_to_int, int_to_weekday,
3838
)
39-
from pandas._libs.tslibs.ccalendar cimport get_days_in_month, dayofweek
39+
from pandas._libs.tslibs.ccalendar cimport DAY_NANOS, get_days_in_month, dayofweek
4040
from pandas._libs.tslibs.conversion cimport (
4141
convert_datetime_to_tsobject,
4242
localize_pydatetime,
@@ -1067,11 +1067,7 @@ cdef class RelativeDeltaOffset(BaseOffset):
10671067

10681068
weeks = kwds.get("weeks", 0) * self.n
10691069
if weeks:
1070-
# integer addition on PeriodIndex is deprecated,
1071-
# so we directly use _time_shift instead
1072-
asper = index.to_period("W")
1073-
shifted = asper._time_shift(weeks)
1074-
index = shifted.to_timestamp() + index.to_perioddelta("W")
1070+
index = index + timedelta(days=7 * weeks)
10751071

10761072
timedelta_kwds = {
10771073
k: v
@@ -1383,7 +1379,9 @@ cdef class BusinessDay(BusinessMixin):
13831379

13841380
@apply_index_wraps
13851381
def apply_index(self, dtindex):
1386-
time = dtindex.to_perioddelta("D")
1382+
i8other = dtindex.asi8
1383+
time = (i8other % DAY_NANOS).view("timedelta64[ns]")
1384+
13871385
# to_period rolls forward to next BDay; track and
13881386
# reduce n where it does when rolling forward
13891387
asper = dtindex.to_period("B")
@@ -2276,6 +2274,7 @@ cdef class SemiMonthOffset(SingleConstructorOffset):
22762274
from pandas import Timedelta
22772275

22782276
dti = dtindex
2277+
i8other = dtindex.asi8
22792278
days_from_start = dtindex.to_perioddelta("M").asi8
22802279
delta = Timedelta(days=self.day_of_month - 1).value
22812280

@@ -2289,7 +2288,7 @@ cdef class SemiMonthOffset(SingleConstructorOffset):
22892288
roll = self._get_roll(dtindex, before_day_of_month, after_day_of_month)
22902289

22912290
# isolate the time since it will be striped away one the next line
2292-
time = dtindex.to_perioddelta("D")
2291+
time = (i8other % DAY_NANOS).view("timedelta64[ns]")
22932292

22942293
# apply the correct number of months
22952294

@@ -2506,10 +2505,9 @@ cdef class Week(SingleConstructorOffset):
25062505
if self.weekday is None:
25072506
# integer addition on PeriodIndex is deprecated,
25082507
# so we use _time_shift directly
2509-
asper = dtindex.to_period("W")
2510-
2511-
shifted = asper._time_shift(self.n)
2512-
return shifted.to_timestamp() + dtindex.to_perioddelta("W")
2508+
td = timedelta(days=7 * self.n)
2509+
td64 = np.timedelta64(td, "ns")
2510+
return dtindex + td64
25132511
else:
25142512
return self._end_apply_index(dtindex)
25152513

@@ -2529,7 +2527,8 @@ cdef class Week(SingleConstructorOffset):
25292527
from pandas import Timedelta
25302528
from .frequencies import get_freq_code # TODO: avoid circular import
25312529

2532-
off = dtindex.to_perioddelta("D")
2530+
i8other = dtindex.asi8
2531+
off = (i8other % DAY_NANOS).view("timedelta64")
25332532

25342533
base, mult = get_freq_code(self.freqstr)
25352534
base_period = dtindex.to_period(base)

0 commit comments

Comments
 (0)