Skip to content

REF: PeriodArray simplify _time_shift #47228

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 5, 2022
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
8 changes: 6 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,9 @@ def __add__(self, other):
# as is_integer returns True for these
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = self._time_shift(other)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other * self.freq.n, operator.add
)

# array-like others
elif is_timedelta64_dtype(other_dtype):
Expand Down Expand Up @@ -1357,7 +1359,9 @@ def __sub__(self, other):
# as is_integer returns True for these
if not is_period_dtype(self.dtype):
raise integer_op_not_supported(self)
result = self._time_shift(-other)
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
other * self.freq.n, operator.sub
)

elif isinstance(other, Period):
result = self._sub_period(other)
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,7 @@ def _time_shift(self, periods: int, freq=None) -> PeriodArray:
"`freq` argument is not supported for "
f"{type(self).__name__}._time_shift"
)
values = self.asi8 + periods * self.freq.n
if self._hasna:
values[self._isnan] = iNaT
return type(self)(values, freq=self.freq)
return self + periods

def _box_func(self, x) -> Period | NaTType:
return Period._from_ordinal(ordinal=x, freq=self.freq)
Expand Down