Skip to content

Commit 256805f

Browse files
authored
REF: PeriodArray simplify _time_shift (#47228)
1 parent 07c46df commit 256805f

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

pandas/core/arrays/datetimelike.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,9 @@ def __add__(self, other):
13011301
# as is_integer returns True for these
13021302
if not is_period_dtype(self.dtype):
13031303
raise integer_op_not_supported(self)
1304-
result = self._time_shift(other)
1304+
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
1305+
other * self.freq.n, operator.add
1306+
)
13051307

13061308
# array-like others
13071309
elif is_timedelta64_dtype(other_dtype):
@@ -1357,7 +1359,9 @@ def __sub__(self, other):
13571359
# as is_integer returns True for these
13581360
if not is_period_dtype(self.dtype):
13591361
raise integer_op_not_supported(self)
1360-
result = self._time_shift(-other)
1362+
result = cast("PeriodArray", self)._addsub_int_array_or_scalar(
1363+
other * self.freq.n, operator.sub
1364+
)
13611365

13621366
elif isinstance(other, Period):
13631367
result = self._sub_period(other)

pandas/core/arrays/period.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,7 @@ def _time_shift(self, periods: int, freq=None) -> PeriodArray:
549549
"`freq` argument is not supported for "
550550
f"{type(self).__name__}._time_shift"
551551
)
552-
values = self.asi8 + periods * self.freq.n
553-
if self._hasna:
554-
values[self._isnan] = iNaT
555-
return type(self)(values, freq=self.freq)
552+
return self + periods
556553

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

0 commit comments

Comments
 (0)