@@ -5,6 +5,7 @@ import warnings
5
5
6
6
from pandas.util._exceptions import find_stack_level
7
7
8
+ from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
8
9
from pandas._libs.tslibs.np_datetime cimport (
9
10
NPY_DATETIMEUNIT,
10
11
get_conversion_factor,
@@ -46,7 +47,7 @@ cdef class PeriodDtypeBase:
46
47
def _resolution_obj(self ) -> "Resolution":
47
48
fgc = self ._freq_group_code
48
49
freq_group = FreqGroup(fgc)
49
- abbrev = _reverse_period_code_map [freq_group.value].split(" -" )[0 ]
50
+ abbrev = _period_code_to_abbrev [freq_group.value].split(" -" )[0 ]
50
51
if abbrev == "B":
51
52
return Resolution.RESO_DAY
52
53
attrname = _abbrev_to_attrnames[abbrev]
@@ -55,7 +56,7 @@ cdef class PeriodDtypeBase:
55
56
@property
56
57
def _freqstr(self ) -> str:
57
58
# Will be passed to to_offset in Period._maybe_convert_freq
58
- out = _reverse_period_code_map .get(self ._dtype_code)
59
+ out = _period_code_to_abbrev .get(self ._dtype_code)
59
60
if self._n == 1:
60
61
return out
61
62
return str(self._n ) + out
@@ -150,27 +151,13 @@ _period_code_map = {
150
151
" ns" : PeriodDtypeCode.N, # Nanosecondly
151
152
}
152
153
153
- _reverse_period_code_map = {
154
+ cdef dict _period_code_to_abbrev = {
154
155
_period_code_map[key]: key for key in _period_code_map}
155
156
156
- # Yearly aliases; careful not to put these in _reverse_period_code_map
157
- _period_code_map.update({"Y" + key[1:]: _period_code_map[key]
158
- for key in _period_code_map
159
- if key.startswith("Y-")})
160
-
161
- _period_code_map.update({
162
- "Q": 2000, # Quarterly - December year end (default quarterly )
163
- "Y": PeriodDtypeCode.A , # Annual
164
- "W": 4000, # Weekly
165
- "C": 5000, # Custom Business Day
166
- })
167
-
168
- cdef set _month_names = {
169
- x.split(" -" )[- 1 ] for x in _period_code_map.keys() if x.startswith(" Y-" )
170
- }
157
+ cdef set _month_names = set (c_MONTH_NUMBERS.keys())
171
158
172
159
# Map attribute-name resolutions to resolution abbreviations
173
- _attrname_to_abbrevs = {
160
+ cdef dict attrname_to_abbrevs = {
174
161
" year" : " Y" ,
175
162
" quarter" : " Q" ,
176
163
" month" : " M" ,
@@ -182,7 +169,6 @@ _attrname_to_abbrevs = {
182
169
" microsecond" : " us" ,
183
170
" nanosecond" : " ns" ,
184
171
}
185
- cdef dict attrname_to_abbrevs = _attrname_to_abbrevs
186
172
cdef dict _abbrev_to_attrnames = {v: k for k, v in attrname_to_abbrevs.items()}
187
173
188
174
OFFSET_TO_PERIOD_FREQSTR: dict = {
@@ -234,7 +220,7 @@ OFFSET_TO_PERIOD_FREQSTR: dict = {
234
220
" YS" : " Y" ,
235
221
" BYS" : " Y" ,
236
222
}
237
- OFFSET_DEPR_FREQSTR: dict[ str , str] = {
223
+ cdef dict c_OFFSET_DEPR_FREQSTR = {
238
224
" M" : " ME" ,
239
225
" Q" : " QE" ,
240
226
" Q-DEC" : " QE-DEC" ,
@@ -277,8 +263,9 @@ OFFSET_DEPR_FREQSTR: dict[str, str]= {
277
263
" A-NOV" : " YE-NOV" ,
278
264
}
279
265
cdef dict c_OFFSET_TO_PERIOD_FREQSTR = OFFSET_TO_PERIOD_FREQSTR
280
- cdef dict c_OFFSET_DEPR_FREQSTR = OFFSET_DEPR_FREQSTR
281
- cdef dict c_REVERSE_OFFSET_DEPR_FREQSTR = {v: k for k, v in OFFSET_DEPR_FREQSTR.items()}
266
+ cdef dict c_REVERSE_OFFSET_DEPR_FREQSTR = {
267
+ v: k for k, v in c_OFFSET_DEPR_FREQSTR.items()
268
+ }
282
269
283
270
cpdef freq_to_period_freqstr(freq_n , freq_name ):
284
271
if freq_n == 1 :
@@ -290,7 +277,7 @@ cpdef freq_to_period_freqstr(freq_n, freq_name):
290
277
return freqstr
291
278
292
279
# Map deprecated resolution abbreviations to correct resolution abbreviations
293
- DEPR_ABBREVS: dict [ str , str ] = {
280
+ cdef dict c_DEPR_ABBREVS = {
294
281
" A" : " Y" ,
295
282
" a" : " Y" ,
296
283
" A-DEC" : " Y-DEC" ,
@@ -359,7 +346,6 @@ DEPR_ABBREVS: dict[str, str]= {
359
346
" N" : " ns" ,
360
347
" n" : " ns" ,
361
348
}
362
- cdef dict c_DEPR_ABBREVS = DEPR_ABBREVS
363
349
364
350
365
351
class FreqGroup (Enum ):
@@ -406,7 +392,7 @@ class Resolution(Enum):
406
392
@property
407
393
def attr_abbrev (self ) -> str:
408
394
# string that we can pass to to_offset
409
- return _attrname_to_abbrevs [self.attrname]
395
+ return attrname_to_abbrevs [self.attrname]
410
396
411
397
@property
412
398
def attrname(self ) -> str:
@@ -450,16 +436,19 @@ class Resolution(Enum):
450
436
>>> Resolution.get_reso_from_freqstr('h') == Resolution.RESO_HR
451
437
True
452
438
"""
439
+ cdef:
440
+ str abbrev
453
441
try:
454
- if freq in DEPR_ABBREVS:
442
+ if freq in c_DEPR_ABBREVS:
443
+ abbrev = c_DEPR_ABBREVS[freq]
455
444
warnings.warn(
456
445
f"\'{freq}\' is deprecated and will be removed in a future "
457
- f"version. Please use \'{DEPR_ABBREVS.get( freq ) }\' "
446
+ f"version. Please use \'{abbrev }\' "
458
447
"instead of \'{freq}\'.",
459
448
FutureWarning ,
460
449
stacklevel = find_stack_level(),
461
450
)
462
- freq = DEPR_ABBREVS[freq]
451
+ freq = abbrev
463
452
attr_name = _abbrev_to_attrnames[freq]
464
453
except KeyError:
465
454
# For quarterly and yearly resolutions , we need to chop off
@@ -470,15 +459,16 @@ class Resolution(Enum):
470
459
if split_freq[1] not in _month_names:
471
460
# i.e. we want e.g. "Q-DEC", not "Q-INVALID"
472
461
raise
473
- if split_freq[0] in DEPR_ABBREVS:
462
+ if split_freq[0] in c_DEPR_ABBREVS:
463
+ abbrev = c_DEPR_ABBREVS[split_freq[0 ]]
474
464
warnings.warn(
475
465
f"\'{split_freq[0]}\' is deprecated and will be removed in a "
476
- f"future version. Please use \'{DEPR_ABBREVS.get(split_freq[0]) }\' "
466
+ f"future version. Please use \'{abbrev }\' "
477
467
f"instead of \'{split_freq[0]}\'.",
478
468
FutureWarning ,
479
469
stacklevel = find_stack_level(),
480
470
)
481
- split_freq[0] = DEPR_ABBREVS[split_freq[0]]
471
+ split_freq[0] = abbrev
482
472
attr_name = _abbrev_to_attrnames[split_freq[0 ]]
483
473
484
474
return cls.from_attrname(attr_name )
0 commit comments