@@ -94,6 +94,7 @@ from pandas._libs.tslibs.offsets cimport (
94
94
is_tick_object,
95
95
is_offset_object,
96
96
)
97
+ from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG
97
98
from pandas._libs.tslibs.tzconversion cimport tz_convert_utc_to_tzlocal
98
99
99
100
@@ -1649,7 +1650,7 @@ cdef class _Period:
1649
1650
freq = self ._maybe_convert_freq(freq)
1650
1651
how = validate_end_alias(how)
1651
1652
base1 = self ._dtype.dtype_code
1652
- base2 , _ = get_freq_code (freq)
1653
+ base2 = freq_to_dtype_code (freq)
1653
1654
1654
1655
# self.n can't be negative or 0
1655
1656
end = how == ' E'
@@ -1739,10 +1740,11 @@ cdef class _Period:
1739
1740
if freq is None:
1740
1741
base = self ._dtype.dtype_code
1741
1742
freq = get_to_timestamp_base(base)
1743
+ base = freq
1742
1744
else:
1743
1745
freq = self ._maybe_convert_freq(freq)
1746
+ base = freq._period_dtype_code
1744
1747
1745
- base , _ = get_freq_code(freq)
1746
1748
val = self .asfreq(freq, how)
1747
1749
1748
1750
dt64 = period_ordinal_to_dt64(val.ordinal, base)
@@ -2386,8 +2388,7 @@ class Period(_Period):
2386
2388
2387
2389
elif is_period_object(value):
2388
2390
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:
2391
2392
ordinal = other.ordinal
2392
2393
freq = other.freq
2393
2394
else :
@@ -2414,6 +2415,7 @@ class Period(_Period):
2414
2415
except KeyError :
2415
2416
raise ValueError (f" Invalid frequency or could not "
2416
2417
f" infer: {reso}" )
2418
+ freq = to_offset(freq)
2417
2419
2418
2420
elif PyDateTime_Check(value):
2419
2421
dt = value
@@ -2432,7 +2434,7 @@ class Period(_Period):
2432
2434
raise ValueError (msg)
2433
2435
2434
2436
if ordinal is None :
2435
- base, _ = get_freq_code (freq)
2437
+ base = freq_to_dtype_code (freq)
2436
2438
ordinal = period_ordinal(dt.year, dt.month, dt.day,
2437
2439
dt.hour, dt.minute, dt.second,
2438
2440
dt.microsecond, 0 , base)
@@ -2444,9 +2446,17 @@ cdef bint is_period_object(object obj):
2444
2446
return isinstance (obj, _Period)
2445
2447
2446
2448
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
+
2447
2456
cdef 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)
2450
2460
if quarter is not None :
2451
2461
year, month = quarter_to_myear(year, quarter, freq)
2452
2462
0 commit comments