Skip to content

Commit ffbc9c8

Browse files
authored
CLN: remove unnecessary bits from dtypes.pyx (#55985)
1 parent 640fe26 commit ffbc9c8

File tree

8 files changed

+30
-57
lines changed

8 files changed

+30
-57
lines changed

Diff for: pandas/_libs/tslibs/ccalendar.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ cpdef int32_t get_day_of_year(int year, int month, int day) noexcept nogil
1515
cpdef int get_lastbday(int year, int month) noexcept nogil
1616
cpdef int get_firstbday(int year, int month) noexcept nogil
1717

18-
cdef dict c_MONTH_NUMBERS
18+
cdef dict c_MONTH_NUMBERS, MONTH_TO_CAL_NUM
1919

2020
cdef int32_t* month_offset

Diff for: pandas/_libs/tslibs/ccalendar.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ MONTHS_FULL = ["", "January", "February", "March", "April", "May", "June",
3838
MONTH_NUMBERS = {name: num for num, name in enumerate(MONTHS)}
3939
cdef dict c_MONTH_NUMBERS = MONTH_NUMBERS
4040
MONTH_ALIASES = {(num + 1): name for num, name in enumerate(MONTHS)}
41-
MONTH_TO_CAL_NUM = {name: num + 1 for num, name in enumerate(MONTHS)}
41+
cdef dict MONTH_TO_CAL_NUM = {name: num + 1 for num, name in enumerate(MONTHS)}
4242

4343
DAYS = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
4444
DAYS_FULL = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",

Diff for: pandas/_libs/tslibs/dtypes.pyi

-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
from enum import Enum
22

3-
from pandas._libs.tslibs.timedeltas import UnitChoices
4-
5-
# These are not public API, but are exposed in the .pyi file because they
6-
# are imported in tests.
7-
_attrname_to_abbrevs: dict[str, str]
8-
_period_code_map: dict[str, int]
93
OFFSET_TO_PERIOD_FREQSTR: dict[str, str]
10-
OFFSET_DEPR_FREQSTR: dict[str, str]
11-
DEPR_ABBREVS: dict[str, UnitChoices]
124

135
def periods_per_day(reso: int) -> int: ...
146
def periods_per_second(reso: int) -> int: ...

Diff for: pandas/_libs/tslibs/dtypes.pyx

+22-32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import warnings
55

66
from pandas.util._exceptions import find_stack_level
77

8+
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
89
from pandas._libs.tslibs.np_datetime cimport (
910
NPY_DATETIMEUNIT,
1011
get_conversion_factor,
@@ -46,7 +47,7 @@ cdef class PeriodDtypeBase:
4647
def _resolution_obj(self) -> "Resolution":
4748
fgc = self._freq_group_code
4849
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]
5051
if abbrev == "B":
5152
return Resolution.RESO_DAY
5253
attrname = _abbrev_to_attrnames[abbrev]
@@ -55,7 +56,7 @@ cdef class PeriodDtypeBase:
5556
@property
5657
def _freqstr(self) -> str:
5758
# 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)
5960
if self._n == 1:
6061
return out
6162
return str(self._n) + out
@@ -150,27 +151,13 @@ _period_code_map = {
150151
"ns": PeriodDtypeCode.N, # Nanosecondly
151152
}
152153

153-
_reverse_period_code_map = {
154+
cdef dict _period_code_to_abbrev = {
154155
_period_code_map[key]: key for key in _period_code_map}
155156

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())
171158

172159
# Map attribute-name resolutions to resolution abbreviations
173-
_attrname_to_abbrevs = {
160+
cdef dict attrname_to_abbrevs = {
174161
"year": "Y",
175162
"quarter": "Q",
176163
"month": "M",
@@ -182,7 +169,6 @@ _attrname_to_abbrevs = {
182169
"microsecond": "us",
183170
"nanosecond": "ns",
184171
}
185-
cdef dict attrname_to_abbrevs = _attrname_to_abbrevs
186172
cdef dict _abbrev_to_attrnames = {v: k for k, v in attrname_to_abbrevs.items()}
187173

188174
OFFSET_TO_PERIOD_FREQSTR: dict = {
@@ -234,7 +220,7 @@ OFFSET_TO_PERIOD_FREQSTR: dict = {
234220
"YS": "Y",
235221
"BYS": "Y",
236222
}
237-
OFFSET_DEPR_FREQSTR: dict[str, str]= {
223+
cdef dict c_OFFSET_DEPR_FREQSTR = {
238224
"M": "ME",
239225
"Q": "QE",
240226
"Q-DEC": "QE-DEC",
@@ -277,8 +263,9 @@ OFFSET_DEPR_FREQSTR: dict[str, str]= {
277263
"A-NOV": "YE-NOV",
278264
}
279265
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+
}
282269

283270
cpdef freq_to_period_freqstr(freq_n, freq_name):
284271
if freq_n == 1:
@@ -290,7 +277,7 @@ cpdef freq_to_period_freqstr(freq_n, freq_name):
290277
return freqstr
291278

292279
# Map deprecated resolution abbreviations to correct resolution abbreviations
293-
DEPR_ABBREVS: dict[str, str]= {
280+
cdef dict c_DEPR_ABBREVS = {
294281
"A": "Y",
295282
"a": "Y",
296283
"A-DEC": "Y-DEC",
@@ -359,7 +346,6 @@ DEPR_ABBREVS: dict[str, str]= {
359346
"N": "ns",
360347
"n": "ns",
361348
}
362-
cdef dict c_DEPR_ABBREVS = DEPR_ABBREVS
363349

364350

365351
class FreqGroup(Enum):
@@ -406,7 +392,7 @@ class Resolution(Enum):
406392
@property
407393
def attr_abbrev(self) -> str:
408394
# string that we can pass to to_offset
409-
return _attrname_to_abbrevs[self.attrname]
395+
return attrname_to_abbrevs[self.attrname]
410396

411397
@property
412398
def attrname(self) -> str:
@@ -450,16 +436,19 @@ class Resolution(Enum):
450436
>>> Resolution.get_reso_from_freqstr('h') == Resolution.RESO_HR
451437
True
452438
"""
439+
cdef:
440+
str abbrev
453441
try:
454-
if freq in DEPR_ABBREVS:
442+
if freq in c_DEPR_ABBREVS:
443+
abbrev = c_DEPR_ABBREVS[freq]
455444
warnings.warn(
456445
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}\' "
458447
"instead of \'{freq}\'.",
459448
FutureWarning,
460449
stacklevel=find_stack_level(),
461450
)
462-
freq = DEPR_ABBREVS[freq]
451+
freq = abbrev
463452
attr_name = _abbrev_to_attrnames[freq]
464453
except KeyError:
465454
# For quarterly and yearly resolutions, we need to chop off
@@ -470,15 +459,16 @@ class Resolution(Enum):
470459
if split_freq[1] not in _month_names:
471460
# i.e. we want e.g. "Q-DEC", not "Q-INVALID"
472461
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]]
474464
warnings.warn(
475465
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}\' "
477467
f"instead of \'{split_freq[0]}\'.",
478468
FutureWarning,
479469
stacklevel=find_stack_level(),
480470
)
481-
split_freq[0] = DEPR_ABBREVS[split_freq[0]]
471+
split_freq[0] = abbrev
482472
attr_name = _abbrev_to_attrnames[split_freq[0]]
483473

484474
return cls.from_attrname(attr_name)

Diff for: pandas/_libs/tslibs/offsets.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ from pandas._libs.tslibs.util cimport (
4242

4343
from pandas._libs.tslibs.ccalendar import (
4444
MONTH_ALIASES,
45-
MONTH_TO_CAL_NUM,
4645
int_to_weekday,
4746
weekday_to_int,
4847
)
4948
from pandas.util._exceptions import find_stack_level
5049

5150
from pandas._libs.tslibs.ccalendar cimport (
51+
MONTH_TO_CAL_NUM,
5252
dayofweek,
5353
get_days_in_month,
5454
get_firstbday,

Diff for: pandas/_libs/tslibs/parsing.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ from dateutil.tz import (
5151

5252
from pandas._config import get_option
5353

54-
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
54+
from pandas._libs.tslibs.ccalendar cimport MONTH_TO_CAL_NUM
5555
from pandas._libs.tslibs.dtypes cimport (
5656
attrname_to_npy_unit,
5757
npy_unit_to_attrname,
@@ -623,7 +623,7 @@ cpdef quarter_to_myear(int year, int quarter, str freq):
623623
raise ValueError("Quarter must be 1 <= q <= 4")
624624

625625
if freq is not None:
626-
mnum = c_MONTH_NUMBERS[get_rule_month(freq)] + 1
626+
mnum = MONTH_TO_CAL_NUM[get_rule_month(freq)]
627627
month = (mnum + (quarter - 1) * 3) % 12 + 1
628628
if month > mnum:
629629
year -= 1

Diff for: pandas/tests/scalar/period/test_asfreq.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22

3-
from pandas._libs.tslibs.dtypes import _period_code_map
43
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
54
from pandas.errors import OutOfBoundsDatetime
65

@@ -828,5 +827,3 @@ def test_asfreq_MS(self):
828827
msg = "MonthBegin is not supported as period frequency"
829828
with pytest.raises(TypeError, match=msg):
830829
Period("2013-01", "MS")
831-
832-
assert _period_code_map.get("MS") is None

Diff for: pandas/tests/tseries/frequencies/test_freq_code.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
Resolution,
77
to_offset,
88
)
9-
from pandas._libs.tslibs.dtypes import _attrname_to_abbrevs
109

1110
import pandas._testing as tm
1211

@@ -40,14 +39,9 @@ def test_get_to_timestamp_base(freqstr, exp_freqstr):
4039
],
4140
)
4241
def test_get_attrname_from_abbrev(freqstr, expected):
43-
assert Resolution.get_reso_from_freqstr(freqstr).attrname == expected
44-
45-
46-
@pytest.mark.parametrize("freq", ["D", "h", "min", "s", "ms", "us", "ns"])
47-
def test_get_freq_roundtrip2(freq):
48-
obj = Resolution.get_reso_from_freqstr(freq)
49-
result = _attrname_to_abbrevs[obj.attrname]
50-
assert freq == result
42+
reso = Resolution.get_reso_from_freqstr(freqstr)
43+
assert reso.attr_abbrev == freqstr
44+
assert reso.attrname == expected
5145

5246

5347
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)