@@ -94,6 +94,7 @@ from pandas._libs.tslibs.offsets cimport (
9494 is_tick_object,
9595 is_offset_object,
9696)
97+ from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG
9798from pandas._libs.tslibs.tzconversion cimport tz_convert_utc_to_tzlocal
9899
99100
@@ -1649,7 +1650,7 @@ cdef class _Period:
16491650 freq = self ._maybe_convert_freq(freq)
16501651 how = validate_end_alias(how)
16511652 base1 = self ._dtype.dtype_code
1652- base2 , _ = get_freq_code (freq)
1653+ base2 = freq_to_dtype_code (freq)
16531654
16541655 # self.n can't be negative or 0
16551656 end = how == ' E'
@@ -1739,10 +1740,11 @@ cdef class _Period:
17391740 if freq is None:
17401741 base = self ._dtype.dtype_code
17411742 freq = get_to_timestamp_base(base)
1743+ base = freq
17421744 else:
17431745 freq = self ._maybe_convert_freq(freq)
1746+ base = freq._period_dtype_code
17441747
1745- base , _ = get_freq_code(freq)
17461748 val = self .asfreq(freq, how)
17471749
17481750 dt64 = period_ordinal_to_dt64(val.ordinal, base)
@@ -2386,8 +2388,7 @@ class Period(_Period):
23862388
23872389 elif is_period_object(value):
23882390 other = value
2389- if freq is None or get_freq_code(
2390- freq) == get_freq_code(other.freq):
2391+ if freq is None or freq._period_dtype_code == other.freq._period_dtype_code:
23912392 ordinal = other.ordinal
23922393 freq = other.freq
23932394 else :
@@ -2414,6 +2415,7 @@ class Period(_Period):
24142415 except KeyError :
24152416 raise ValueError (f" Invalid frequency or could not "
24162417 f" infer: {reso}" )
2418+ freq = to_offset(freq)
24172419
24182420 elif PyDateTime_Check(value):
24192421 dt = value
@@ -2432,7 +2434,7 @@ class Period(_Period):
24322434 raise ValueError (msg)
24332435
24342436 if ordinal is None :
2435- base, _ = get_freq_code (freq)
2437+ base = freq_to_dtype_code (freq)
24362438 ordinal = period_ordinal(dt.year, dt.month, dt.day,
24372439 dt.hour, dt.minute, dt.second,
24382440 dt.microsecond, 0 , base)
@@ -2444,9 +2446,17 @@ cdef bint is_period_object(object obj):
24442446 return isinstance (obj, _Period)
24452447
24462448
2449+ cpdef int freq_to_dtype_code(BaseOffset freq) except ? - 1 :
2450+ try :
2451+ return freq._period_dtype_code
2452+ except AttributeError as err:
2453+ raise ValueError (INVALID_FREQ_ERR_MSG) from err
2454+
2455+
24472456cdef int64_t _ordinal_from_fields(int year, int month, quarter, int day,
2448- int hour, int minute, int second, freq):
2449- base, mult = get_freq_code(freq)
2457+ int hour, int minute, int second,
2458+ BaseOffset freq):
2459+ base = freq_to_dtype_code(freq)
24502460 if quarter is not None :
24512461 year, month = quarter_to_myear(year, quarter, freq)
24522462
0 commit comments