Skip to content

Commit a2710ce

Browse files
jbrockmendelpull[bot]
authored andcommitted
REF: move _sub_period_array implementation to PeriodArray (#34103)
1 parent fc4047b commit a2710ce

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

pandas/core/arrays/datetimelike.py

+6-37
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import numpy as np
77

8-
from pandas._libs import NaT, NaTType, Timestamp, algos, iNaT, lib
9-
from pandas._libs.tslibs.period import DIFFERENT_FREQ, IncompatibleFrequency, Period
8+
from pandas._libs import NaT, NaTType, Period, Timestamp, algos, iNaT, lib
109
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds
1110
from pandas._libs.tslibs.timestamps import (
1211
RoundTo,
@@ -739,7 +738,7 @@ def _validate_shift_value(self, fill_value):
739738
# only warn if we're not going to raise
740739
if self._scalar_type is Period and lib.is_integer(fill_value):
741740
# kludge for #31971 since Period(integer) tries to cast to str
742-
new_fill = Period._from_ordinal(fill_value, freq=self.freq)
741+
new_fill = Period._from_ordinal(fill_value, freq=self.dtype.freq)
743742
else:
744743
new_fill = self._scalar_type(fill_value)
745744

@@ -1095,7 +1094,7 @@ def _resolution(self):
10951094
return frequencies.Resolution.get_reso_from_freq(self.freqstr)
10961095

10971096
@property # NB: override with cache_readonly in immutable subclasses
1098-
def resolution(self):
1097+
def resolution(self) -> str:
10991098
"""
11001099
Returns day, hour, minute, second, millisecond or microsecond
11011100
"""
@@ -1278,41 +1277,11 @@ def _sub_nat(self):
12781277
return result.view("timedelta64[ns]")
12791278

12801279
def _sub_period_array(self, other):
1281-
"""
1282-
Subtract a Period Array/Index from self. This is only valid if self
1283-
is itself a Period Array/Index, raises otherwise. Both objects must
1284-
have the same frequency.
1285-
1286-
Parameters
1287-
----------
1288-
other : PeriodIndex or PeriodArray
1289-
1290-
Returns
1291-
-------
1292-
result : np.ndarray[object]
1293-
Array of DateOffset objects; nulls represented by NaT.
1294-
"""
1295-
if not is_period_dtype(self.dtype):
1296-
raise TypeError(
1297-
f"cannot subtract {other.dtype}-dtype from {type(self).__name__}"
1298-
)
1299-
1300-
if self.freq != other.freq:
1301-
msg = DIFFERENT_FREQ.format(
1302-
cls=type(self).__name__, own_freq=self.freqstr, other_freq=other.freqstr
1303-
)
1304-
raise IncompatibleFrequency(msg)
1305-
1306-
new_values = checked_add_with_arr(
1307-
self.asi8, -other.asi8, arr_mask=self._isnan, b_mask=other._isnan
1280+
# Overridden by PeriodArray
1281+
raise TypeError(
1282+
f"cannot subtract {other.dtype}-dtype from {type(self).__name__}"
13081283
)
13091284

1310-
new_values = np.array([self.freq.base * x for x in new_values])
1311-
if self._hasnans or other._hasnans:
1312-
mask = (self._isnan) | (other._isnan)
1313-
new_values[mask] = NaT
1314-
return new_values
1315-
13161285
def _addsub_object_array(self, other: np.ndarray, op):
13171286
"""
13181287
Add or subtract array-like of DateOffset objects

pandas/core/arrays/period.py

+31
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,37 @@ def _sub_period(self, other):
604604

605605
return new_data
606606

607+
def _sub_period_array(self, other):
608+
"""
609+
Subtract a Period Array/Index from self. This is only valid if self
610+
is itself a Period Array/Index, raises otherwise. Both objects must
611+
have the same frequency.
612+
613+
Parameters
614+
----------
615+
other : PeriodIndex or PeriodArray
616+
617+
Returns
618+
-------
619+
result : np.ndarray[object]
620+
Array of DateOffset objects; nulls represented by NaT.
621+
"""
622+
if self.freq != other.freq:
623+
msg = DIFFERENT_FREQ.format(
624+
cls=type(self).__name__, own_freq=self.freqstr, other_freq=other.freqstr
625+
)
626+
raise IncompatibleFrequency(msg)
627+
628+
new_values = algos.checked_add_with_arr(
629+
self.asi8, -other.asi8, arr_mask=self._isnan, b_mask=other._isnan
630+
)
631+
632+
new_values = np.array([self.freq.base * x for x in new_values])
633+
if self._hasnans or other._hasnans:
634+
mask = (self._isnan) | (other._isnan)
635+
new_values[mask] = NaT
636+
return new_values
637+
607638
def _addsub_int_array(
608639
self, other: np.ndarray, op: Callable[[Any, Any], Any],
609640
) -> "PeriodArray":

0 commit comments

Comments
 (0)