@@ -62,15 +62,11 @@ def wrapper(self, other):
62
62
other = other ._values
63
63
64
64
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 )
68
66
69
67
result = op (other .ordinal )
70
68
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 )
74
70
75
71
if not_implemented :
76
72
return NotImplemented
@@ -241,6 +237,11 @@ def _generate_range(cls, start, end, periods, freq, fields):
241
237
242
238
return subarr , freq
243
239
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
+
244
245
# --------------------------------------------------------------------
245
246
# Data / Attributes
246
247
@@ -395,10 +396,7 @@ def _validate_fill_value(self, fill_value):
395
396
if isna (fill_value ):
396
397
fill_value = iNaT
397
398
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 )
402
400
fill_value = fill_value .ordinal
403
401
else :
404
402
raise ValueError ("'fill_value' should be a Period. "
@@ -667,10 +665,7 @@ def _sub_datelike(self, other):
667
665
def _sub_period (self , other ):
668
666
# If the operation is well-defined, we return an object-Index
669
667
# 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 )
674
669
asi8 = self .asi8
675
670
new_data = asi8 - other .ordinal
676
671
new_data = np .array ([self .freq * x for x in new_data ])
0 commit comments