diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index f4bdd44b9ec39..178efbe6f8376 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -62,15 +62,11 @@ def wrapper(self, other): other = other._values if isinstance(other, Period): - if other.freq != self.freq: - msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) - raise IncompatibleFrequency(msg) + self._check_compatible_with(other) result = op(other.ordinal) elif isinstance(other, cls): - if other.freq != self.freq: - msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) - raise IncompatibleFrequency(msg) + self._check_compatible_with(other) if not_implemented: return NotImplemented @@ -241,6 +237,11 @@ def _generate_range(cls, start, end, periods, freq, fields): return subarr, freq + def _check_compatible_with(self, other): + if self.freqstr != other.freqstr: + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) + raise IncompatibleFrequency(msg) + # -------------------------------------------------------------------- # Data / Attributes @@ -395,10 +396,7 @@ def _validate_fill_value(self, fill_value): if isna(fill_value): fill_value = iNaT elif isinstance(fill_value, Period): - if fill_value.freq != self.freq: - msg = DIFFERENT_FREQ_INDEX.format(self.freq.freqstr, - fill_value.freqstr) - raise IncompatibleFrequency(msg) + self._check_compatible_with(fill_value) fill_value = fill_value.ordinal else: raise ValueError("'fill_value' should be a Period. " @@ -667,10 +665,7 @@ def _sub_datelike(self, other): def _sub_period(self, other): # If the operation is well-defined, we return an object-Index # of DateOffsets. Null entries are filled with pd.NaT - if self.freq != other.freq: - msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) - raise IncompatibleFrequency(msg) - + self._check_compatible_with(other) asi8 = self.asi8 new_data = asi8 - other.ordinal new_data = np.array([self.freq * x for x in new_data])