Skip to content

Commit 5af32e2

Browse files
authored
REF: avoid get_freq_code (#34659)
1 parent 8417b4b commit 5af32e2

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

pandas/_libs/tslibs/period.pyx

+17-7
Original file line numberDiff line numberDiff line change
@@ -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
9798
from 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+
24472456
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)
24502460
if quarter is not None:
24512461
year, month = quarter_to_myear(year, quarter, freq)
24522462

pandas/core/arrays/period.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,11 @@ def to_timestamp(self, freq=None, how="start"):
442442
if freq is None:
443443
base = self.freq._period_dtype_code
444444
freq = libfrequencies.get_to_timestamp_base(base)
445+
base = freq
445446
else:
446447
freq = Period._maybe_convert_freq(freq)
448+
base = freq._period_dtype_code
447449

448-
base, _ = libfrequencies.get_freq_code(freq)
449450
new_data = self.asfreq(freq, how=how)
450451

451452
new_data = libperiod.periodarr_to_dt64arr(new_data.asi8, base)
@@ -962,7 +963,8 @@ def _get_ordinal_range(start, end, periods, freq, mult=1):
962963
)
963964

964965
if freq is not None:
965-
_, mult = libfrequencies.get_freq_code(freq)
966+
freq = to_offset(freq)
967+
mult = freq.n
966968

967969
if start is not None:
968970
start = Period(start, freq)
@@ -1024,10 +1026,11 @@ def _range_from_fields(
10241026

10251027
if quarter is not None:
10261028
if freq is None:
1027-
freq = "Q"
1029+
freq = to_offset("Q")
10281030
base = libfrequencies.FreqGroup.FR_QTR
10291031
else:
1030-
base, mult = libfrequencies.get_freq_code(freq)
1032+
freq = to_offset(freq)
1033+
base = libperiod.freq_to_dtype_code(freq)
10311034
if base != libfrequencies.FreqGroup.FR_QTR:
10321035
raise AssertionError("base must equal FR_QTR")
10331036

@@ -1037,7 +1040,8 @@ def _range_from_fields(
10371040
val = libperiod.period_ordinal(y, m, 1, 1, 1, 1, 0, 0, base)
10381041
ordinals.append(val)
10391042
else:
1040-
base, mult = libfrequencies.get_freq_code(freq)
1043+
freq = to_offset(freq)
1044+
base = libperiod.freq_to_dtype_code(freq)
10411045
arrays = _make_field_arrays(year, month, day, hour, minute, second)
10421046
for y, mth, d, h, mn, s in zip(*arrays):
10431047
ordinals.append(libperiod.period_ordinal(y, mth, d, h, mn, s, 0, 0, base))

0 commit comments

Comments
 (0)