7
7
from pandas ._libs .tslibs import NaT , iNaT , period as libperiod
8
8
from pandas ._libs .tslibs .fields import isleapyear_arr
9
9
from pandas ._libs .tslibs .period import (
10
- DIFFERENT_FREQ_INDEX , IncompatibleFrequency , Period , get_period_field_arr ,
10
+ DIFFERENT_FREQ , IncompatibleFrequency , Period , get_period_field_arr ,
11
11
period_asfreq_arr )
12
12
from pandas ._libs .tslibs .timedeltas import Timedelta , delta_to_nanoseconds
13
13
import pandas .compat as compat
30
30
from pandas .core .missing import backfill_1d , pad_1d
31
31
32
32
from pandas .tseries import frequencies
33
- from pandas .tseries .offsets import Tick
33
+ from pandas .tseries .offsets import DateOffset , Tick , _delta_to_tick
34
34
35
35
36
36
def _field_accessor (name , alias , docstring = None ):
@@ -166,8 +166,9 @@ def __init__(self, values, freq=None, dtype=None, copy=False):
166
166
167
167
if isinstance (values , type (self )):
168
168
if freq is not None and freq != values .freq :
169
- msg = DIFFERENT_FREQ_INDEX .format (values .freq .freqstr ,
170
- freq .freqstr )
169
+ msg = DIFFERENT_FREQ .format (cls = type (self ).__name__ ,
170
+ own_freq = values .freq .freqstr ,
171
+ other_freq = freq .freqstr )
171
172
raise IncompatibleFrequency (msg )
172
173
values , freq = values ._data , values .freq
173
174
@@ -239,8 +240,7 @@ def _generate_range(cls, start, end, periods, freq, fields):
239
240
240
241
def _check_compatible_with (self , other ):
241
242
if self .freqstr != other .freqstr :
242
- msg = DIFFERENT_FREQ_INDEX .format (self .freqstr , other .freqstr )
243
- raise IncompatibleFrequency (msg )
243
+ _raise_on_incompatible (self , other )
244
244
245
245
# --------------------------------------------------------------------
246
246
# Data / Attributes
@@ -372,15 +372,13 @@ def __setitem__(
372
372
value = period_array (value )
373
373
374
374
if self .freqstr != value .freqstr :
375
- msg = DIFFERENT_FREQ_INDEX .format (self .freqstr , value .freqstr )
376
- raise IncompatibleFrequency (msg )
375
+ _raise_on_incompatible (self , value )
377
376
378
377
value = value .asi8
379
378
elif isinstance (value , Period ):
380
379
381
380
if self .freqstr != value .freqstr :
382
- msg = DIFFERENT_FREQ_INDEX .format (self .freqstr , value .freqstr )
383
- raise IncompatibleFrequency (msg )
381
+ _raise_on_incompatible (self , value )
384
382
385
383
value = value .ordinal
386
384
elif isna (value ):
@@ -696,8 +694,7 @@ def _add_offset(self, other):
696
694
assert not isinstance (other , Tick )
697
695
base = frequencies .get_base_alias (other .rule_code )
698
696
if base != self .freq .rule_code :
699
- msg = DIFFERENT_FREQ_INDEX .format (self .freqstr , other .freqstr )
700
- raise IncompatibleFrequency (msg )
697
+ _raise_on_incompatible (self , other )
701
698
702
699
# Note: when calling parent class's _add_timedeltalike_scalar,
703
700
# it will call delta_to_nanoseconds(delta). Because delta here
@@ -760,10 +757,7 @@ def _add_delta(self, other):
760
757
"""
761
758
if not isinstance (self .freq , Tick ):
762
759
# We cannot add timedelta-like to non-tick PeriodArray
763
- raise IncompatibleFrequency ("Input has different freq from "
764
- "{cls}(freq={freqstr})"
765
- .format (cls = type (self ).__name__ ,
766
- freqstr = self .freqstr ))
760
+ _raise_on_incompatible (self , other )
767
761
768
762
new_ordinals = super (PeriodArray , self )._add_delta (other )
769
763
return type (self )(new_ordinals , freq = self .freq )
@@ -815,10 +809,7 @@ def _check_timedeltalike_freq_compat(self, other):
815
809
# by which will be added to self.
816
810
return delta
817
811
818
- raise IncompatibleFrequency ("Input has different freq from "
819
- "{cls}(freq={freqstr})"
820
- .format (cls = type (self ).__name__ ,
821
- freqstr = self .freqstr ))
812
+ _raise_on_incompatible (self , other )
822
813
823
814
def _values_for_argsort (self ):
824
815
return self ._data
@@ -827,6 +818,34 @@ def _values_for_argsort(self):
827
818
PeriodArray ._add_comparison_ops ()
828
819
829
820
821
+ def _raise_on_incompatible (left , right ):
822
+ """
823
+ Helper function to render a consistent error message when raising
824
+ IncompatibleFrequency.
825
+
826
+ Parameters
827
+ ----------
828
+ left : PeriodArray
829
+ right : DateOffset, Period, ndarray, or timedelta-like
830
+
831
+ Raises
832
+ ------
833
+ IncompatibleFrequency
834
+ """
835
+ # GH#24283 error message format depends on whether right is scalar
836
+ if isinstance (right , np .ndarray ):
837
+ other_freq = None
838
+ elif isinstance (right , (ABCPeriodIndex , PeriodArray , Period , DateOffset )):
839
+ other_freq = right .freqstr
840
+ else :
841
+ other_freq = _delta_to_tick (Timedelta (right )).freqstr
842
+
843
+ msg = DIFFERENT_FREQ .format (cls = type (left ).__name__ ,
844
+ own_freq = left .freqstr ,
845
+ other_freq = other_freq )
846
+ raise IncompatibleFrequency (msg )
847
+
848
+
830
849
# -------------------------------------------------------------------
831
850
# Constructor Helpers
832
851
0 commit comments