Skip to content

Commit fc37087

Browse files
authored
REF: implement c_FreqGroup, FreqGroup in tslibs.dtypes (#34588)
1 parent 23aec3a commit fc37087

File tree

4 files changed

+65
-63
lines changed

4 files changed

+65
-63
lines changed

pandas/_libs/tslibs/dtypes.pxd

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11

2+
cdef enum c_FreqGroup:
3+
# Mirrors FreqGroup in the .pxy file
4+
FR_ANN = 1000
5+
FR_QTR = 2000
6+
FR_MTH = 3000
7+
FR_WK = 4000
8+
FR_BUS = 5000
9+
FR_DAY = 6000
10+
FR_HR = 7000
11+
FR_MIN = 8000
12+
FR_SEC = 9000
13+
FR_MS = 10000
14+
FR_US = 11000
15+
FR_NS = 12000
16+
FR_UND = -10000 # undefined
17+
18+
219
cdef enum PeriodDtypeCode:
320
# Annual freqs with various fiscal year ends.
421
# eg, 2005 for A_FEB runs Mar 1, 2004 to Feb 28, 2005

pandas/_libs/tslibs/dtypes.pyx

+17
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,20 @@ _period_code_map.update({
106106
"W": 4000, # Weekly
107107
"C": 5000, # Custom Business Day
108108
})
109+
110+
111+
class FreqGroup:
112+
# Mirrors c_FreqGroup in the .pxd file
113+
FR_ANN = 1000
114+
FR_QTR = 2000
115+
FR_MTH = 3000
116+
FR_WK = 4000
117+
FR_BUS = 5000
118+
FR_DAY = 6000
119+
FR_HR = 7000
120+
FR_MIN = 8000
121+
FR_SEC = 9000
122+
FR_MS = 10000
123+
FR_US = 11000
124+
FR_NS = 12000
125+
FR_UND = -10000 # undefined

pandas/_libs/tslibs/frequencies.pyx

+1-16
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,12 @@ from pandas._libs.tslibs.offsets import (
1212
opattern,
1313
)
1414

15-
from .dtypes import _period_code_map, _reverse_period_code_map
15+
from .dtypes import FreqGroup, _period_code_map, _reverse_period_code_map
1616

1717
# ---------------------------------------------------------------------
1818
# Period codes
1919

2020

21-
class FreqGroup:
22-
FR_ANN = 1000
23-
FR_QTR = 2000
24-
FR_MTH = 3000
25-
FR_WK = 4000
26-
FR_BUS = 5000
27-
FR_DAY = 6000
28-
FR_HR = 7000
29-
FR_MIN = 8000
30-
FR_SEC = 9000
31-
FR_MS = 10000
32-
FR_US = 11000
33-
FR_NS = 12000
34-
35-
3621
# Map attribute-name resolutions to resolution abbreviations
3722
_attrname_to_abbrevs = {
3823
"year": "A",

pandas/_libs/tslibs/period.pyx

+30-47
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,22 @@ from pandas._libs.tslibs.ccalendar cimport (
5656
)
5757
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
5858

59-
from pandas._libs.tslibs.dtypes cimport PeriodDtypeBase
59+
from pandas._libs.tslibs.dtypes cimport (
60+
PeriodDtypeBase,
61+
FR_UND,
62+
FR_ANN,
63+
FR_QTR,
64+
FR_MTH,
65+
FR_WK,
66+
FR_BUS,
67+
FR_DAY,
68+
FR_HR,
69+
FR_MIN,
70+
FR_SEC,
71+
FR_MS,
72+
FR_US,
73+
FR_NS,
74+
)
6075

6176
from pandas._libs.tslibs.frequencies cimport (
6277
attrname_to_abbrevs,
@@ -98,23 +113,6 @@ ctypedef int64_t (*freq_conv_func)(int64_t, asfreq_info*) nogil
98113

99114
cdef extern from *:
100115
"""
101-
/*** FREQUENCY CONSTANTS ***/
102-
// See frequencies.pyx for more detailed variants
103-
104-
#define FR_ANN 1000 /* Annual */
105-
#define FR_QTR 2000 /* Quarterly - December year end (default Q) */
106-
#define FR_MTH 3000 /* Monthly */
107-
#define FR_WK 4000 /* Weekly */
108-
#define FR_BUS 5000 /* Business days */
109-
#define FR_DAY 6000 /* Daily */
110-
#define FR_HR 7000 /* Hourly */
111-
#define FR_MIN 8000 /* Minutely */
112-
#define FR_SEC 9000 /* Secondly */
113-
#define FR_MS 10000 /* Millisecondly */
114-
#define FR_US 11000 /* Microsecondly */
115-
#define FR_NS 12000 /* Nanosecondly */
116-
#define FR_UND -10000 /* Undefined */
117-
118116
// must use npy typedef b/c int64_t is aliased in cython-generated c
119117
// unclear why we need LL for that row.
120118
// see https://github.com/pandas-dev/pandas/pull/34416/
@@ -128,20 +126,6 @@ cdef extern from *:
128126
{0, 0, 0, 0, 0, 0, 1}};
129127
"""
130128
int64_t daytime_conversion_factor_matrix[7][7]
131-
# TODO: Can we get these frequencies from frequencies.FreqGroup?
132-
int FR_ANN
133-
int FR_QTR
134-
int FR_MTH
135-
int FR_WK
136-
int FR_DAY
137-
int FR_HR
138-
int FR_MIN
139-
int FR_SEC
140-
int FR_MS
141-
int FR_US
142-
int FR_NS
143-
int FR_BUS
144-
int FR_UND
145129

146130

147131
cdef int max_value(int left, int right) nogil:
@@ -1157,30 +1141,29 @@ cdef str period_format(int64_t value, int freq, object fmt=None):
11571141

11581142
if fmt is None:
11591143
freq_group = get_freq_group(freq)
1160-
if freq_group == 1000: # FR_ANN
1144+
if freq_group == FR_ANN:
11611145
fmt = b'%Y'
1162-
elif freq_group == 2000: # FR_QTR
1146+
elif freq_group == FR_QTR:
11631147
fmt = b'%FQ%q'
1164-
elif freq_group == 3000: # FR_MTH
1148+
elif freq_group == FR_MTH:
11651149
fmt = b'%Y-%m'
1166-
elif freq_group == 4000: # WK
1167-
left = period_asfreq(value, freq, 6000, 0)
1168-
right = period_asfreq(value, freq, 6000, 1)
1169-
return f"{period_format(left, 6000)}/{period_format(right, 6000)}"
1170-
elif (freq_group == 5000 # BUS
1171-
or freq_group == 6000): # DAY
1150+
elif freq_group == FR_WK:
1151+
left = period_asfreq(value, freq, FR_DAY, 0)
1152+
right = period_asfreq(value, freq, FR_DAY, 1)
1153+
return f"{period_format(left, FR_DAY)}/{period_format(right, FR_DAY)}"
1154+
elif freq_group == FR_BUS or freq_group == FR_DAY:
11721155
fmt = b'%Y-%m-%d'
1173-
elif freq_group == 7000: # HR
1156+
elif freq_group == FR_HR:
11741157
fmt = b'%Y-%m-%d %H:00'
1175-
elif freq_group == 8000: # MIN
1158+
elif freq_group == FR_MIN:
11761159
fmt = b'%Y-%m-%d %H:%M'
1177-
elif freq_group == 9000: # SEC
1160+
elif freq_group == FR_SEC:
11781161
fmt = b'%Y-%m-%d %H:%M:%S'
1179-
elif freq_group == 10000: # MILLISEC
1162+
elif freq_group == FR_MS:
11801163
fmt = b'%Y-%m-%d %H:%M:%S.%l'
1181-
elif freq_group == 11000: # MICROSEC
1164+
elif freq_group == FR_US:
11821165
fmt = b'%Y-%m-%d %H:%M:%S.%u'
1183-
elif freq_group == 12000: # NANOSEC
1166+
elif freq_group == FR_NS:
11841167
fmt = b'%Y-%m-%d %H:%M:%S.%n'
11851168
else:
11861169
raise ValueError(f"Unknown freq: {freq}")

0 commit comments

Comments
 (0)