Skip to content

REF: avoid use of to_perioddelta #34539

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 5 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
REF: return ndarray from to_perioddelta; use it less
  • Loading branch information
jbrockmendel committed Jun 2, 2020
commit 01943cde338b6d4927cae503a1736f1b6aa5f82e
33 changes: 16 additions & 17 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ from pandas._libs.tslibs.base cimport ABCTimestamp
from pandas._libs.tslibs.ccalendar import (
MONTH_ALIASES, MONTH_TO_CAL_NUM, weekday_to_int, int_to_weekday,
)
from pandas._libs.tslibs.ccalendar cimport get_days_in_month, dayofweek
from pandas._libs.tslibs.ccalendar cimport get_days_in_month, dayofweek, DAY_NANOS
from pandas._libs.tslibs.conversion cimport (
convert_datetime_to_tsobject,
localize_pydatetime,
Expand Down Expand Up @@ -1046,6 +1046,8 @@ cdef class RelativeDeltaOffset(BaseOffset):
-------
DatetimeIndex
"""
from pandas._libs.tslibs.timedeltas import Timedelta

kwds = self.kwds
relativedelta_fast = {
"years",
Expand All @@ -1067,19 +1069,14 @@ cdef class RelativeDeltaOffset(BaseOffset):

weeks = kwds.get("weeks", 0) * self.n
if weeks:
# integer addition on PeriodIndex is deprecated,
# so we directly use _time_shift instead
asper = index.to_period("W")
shifted = asper._time_shift(weeks)
index = shifted.to_timestamp() + index.to_perioddelta("W")
index = index + Timedelta(days=7*weeks)

timedelta_kwds = {
k: v
for k, v in kwds.items()
if k in ["days", "hours", "minutes", "seconds", "microseconds"]
}
if timedelta_kwds:
from .timedeltas import Timedelta
delta = Timedelta(**timedelta_kwds)
index = index + (self.n * delta)
return index
Expand Down Expand Up @@ -1383,13 +1380,15 @@ cdef class BusinessDay(BusinessMixin):

@apply_index_wraps
def apply_index(self, dtindex):
time = dtindex.to_perioddelta("D")
i8other = dtindex.asi8
time = (i8other % DAY_NANOS).view("timedelta64[ns]")

# to_period rolls forward to next BDay; track and
# reduce n where it does when rolling forward
asper = dtindex.to_period("B")

if self.n > 0:
shifted = (dtindex.to_perioddelta("B") - time).asi8 != 0
shifted = (dtindex.to_perioddelta("B") - time).view("i8") != 0

# Integer-array addition is deprecated, so we use
# _time_shift directly
Expand Down Expand Up @@ -2275,8 +2274,9 @@ cdef class SemiMonthOffset(SingleConstructorOffset):
# determine how many days away from the 1st of the month we are
from pandas import Timedelta

i8other = dtindex.asi8
dti = dtindex
days_from_start = dtindex.to_perioddelta("M").asi8
days_from_start = dtindex.to_perioddelta("M").view("i8")
delta = Timedelta(days=self.day_of_month - 1).value

# get boolean array for each element before the day_of_month
Expand All @@ -2289,7 +2289,7 @@ cdef class SemiMonthOffset(SingleConstructorOffset):
roll = self._get_roll(dtindex, before_day_of_month, after_day_of_month)

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

# apply the correct number of months

Expand Down Expand Up @@ -2504,12 +2504,10 @@ 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
asper = dtindex.to_period("W")
from pandas._libs.tslibs.timedeltas import Timedelta

shifted = asper._time_shift(self.n)
return shifted.to_timestamp() + dtindex.to_perioddelta("W")
result = np.asarray(dtindex) + Timedelta(days=7 * self.n)
return type(dtindex)._simple_new(result)
else:
return self._end_apply_index(dtindex)

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

off = dtindex.to_perioddelta("D")
i8other = dtindex.asi8
off = (i8other % DAY_NANOS).view("timedelta64[ns]")

base, mult = get_freq_code(self.freqstr)
base_period = dtindex.to_period(base)
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,14 +1122,13 @@ def to_perioddelta(self, freq):

Returns
-------
TimedeltaArray/Index
ndarray[timedelta64[ns]]
"""
# TODO: consider privatizing (discussion in GH#23113)
from pandas.core.arrays.timedeltas import TimedeltaArray

i8delta = self.asi8 - self.to_period(freq).to_timestamp().asi8
m8delta = i8delta.view("m8[ns]")
return TimedeltaArray(m8delta)
return m8delta

# -----------------------------------------------------------------
# Properties - Vectorized Timestamp Properties/Methods
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def test_to_perioddelta(self, datetime_index, freqstr):

expected = dti.to_perioddelta(freq=freqstr)
result = arr.to_perioddelta(freq=freqstr)
assert isinstance(result, TimedeltaArray)
assert isinstance(result, np.ndarray)

# placeholder until these become actual EA subclasses and we can use
# an EA-specific tm.assert_ function
Expand Down