Skip to content

Commit 1e658f7

Browse files
REF: Consolidate PeriodArray frequency checks (#24285)
1 parent 4efc819 commit 1e658f7

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

pandas/core/arrays/period.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,11 @@ def wrapper(self, other):
6262
other = other._values
6363

6464
if isinstance(other, Period):
65-
if other.freq != self.freq:
66-
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
67-
raise IncompatibleFrequency(msg)
65+
self._check_compatible_with(other)
6866

6967
result = op(other.ordinal)
7068
elif isinstance(other, cls):
71-
if other.freq != self.freq:
72-
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
73-
raise IncompatibleFrequency(msg)
69+
self._check_compatible_with(other)
7470

7571
if not_implemented:
7672
return NotImplemented
@@ -241,6 +237,11 @@ def _generate_range(cls, start, end, periods, freq, fields):
241237

242238
return subarr, freq
243239

240+
def _check_compatible_with(self, other):
241+
if self.freqstr != other.freqstr:
242+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
243+
raise IncompatibleFrequency(msg)
244+
244245
# --------------------------------------------------------------------
245246
# Data / Attributes
246247

@@ -395,10 +396,7 @@ def _validate_fill_value(self, fill_value):
395396
if isna(fill_value):
396397
fill_value = iNaT
397398
elif isinstance(fill_value, Period):
398-
if fill_value.freq != self.freq:
399-
msg = DIFFERENT_FREQ_INDEX.format(self.freq.freqstr,
400-
fill_value.freqstr)
401-
raise IncompatibleFrequency(msg)
399+
self._check_compatible_with(fill_value)
402400
fill_value = fill_value.ordinal
403401
else:
404402
raise ValueError("'fill_value' should be a Period. "
@@ -667,10 +665,7 @@ def _sub_datelike(self, other):
667665
def _sub_period(self, other):
668666
# If the operation is well-defined, we return an object-Index
669667
# of DateOffsets. Null entries are filled with pd.NaT
670-
if self.freq != other.freq:
671-
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
672-
raise IncompatibleFrequency(msg)
673-
668+
self._check_compatible_with(other)
674669
asi8 = self.asi8
675670
new_data = asi8 - other.ordinal
676671
new_data = np.array([self.freq * x for x in new_data])

0 commit comments

Comments
 (0)