Skip to content

Commit a0b99ef

Browse files
authored
REF: de-duplicate hard-coded unit code (#46338)
* REF: de-duplicate hard-coded unit code * typo fixup * mypy fixup
1 parent 0ae9b4d commit a0b99ef

File tree

13 files changed

+208
-228
lines changed

13 files changed

+208
-228
lines changed

pandas/_libs/tslib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def array_with_unit_to_datetime(
243243
)
244244
return result, tz
245245

246-
m, p = precision_from_unit(unit)
246+
m, _ = precision_from_unit(unit)
247247

248248
if is_raise:
249249
# try a quick conversion to i8/f8

pandas/_libs/tslibs/dtypes.pxd

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT
2+
3+
4+
cdef str npy_unit_to_abbrev(NPY_DATETIMEUNIT unit)
5+
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil
6+
17
cdef dict attrname_to_abbrevs
28

39
cdef enum c_FreqGroup:
4-
# Mirrors FreqGroup in the .pxy file
10+
# Mirrors FreqGroup in the .pyx file
511
FR_ANN = 1000
612
FR_QTR = 2000
713
FR_MTH = 3000
@@ -17,6 +23,20 @@ cdef enum c_FreqGroup:
1723
FR_UND = -10000 # undefined
1824

1925

26+
cdef enum c_Resolution:
27+
# Mirrors Resolution in the .pyx file
28+
RESO_NS = 0
29+
RESO_US = 1
30+
RESO_MS = 2
31+
RESO_SEC = 3
32+
RESO_MIN = 4
33+
RESO_HR = 5
34+
RESO_DAY = 6
35+
RESO_MTH = 7
36+
RESO_QTR = 8
37+
RESO_YR = 9
38+
39+
2040
cdef enum PeriodDtypeCode:
2141
# Annual freqs with various fiscal year ends.
2242
# eg, 2005 for A_FEB runs Mar 1, 2004 to Feb 28, 2005

pandas/_libs/tslibs/dtypes.pyi

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ from enum import Enum
22

33
from pandas._libs.tslibs.offsets import BaseOffset
44

5+
# These are not public API, but are exposed in the .pyi file because they
6+
# are imported in tests.
57
_attrname_to_abbrevs: dict[str, str]
68
_period_code_map: dict[str, int]
79

@@ -10,13 +12,11 @@ class PeriodDtypeBase:
1012

1113
# actually __cinit__
1214
def __new__(cls, code: int): ...
13-
def freq_group_code(self) -> int: ...
14-
def date_offset(self) -> BaseOffset: ...
15-
@classmethod
16-
def from_date_offset(cls, offset: BaseOffset) -> PeriodDtypeBase: ...
15+
def _freq_group_code(self) -> int: ...
1716
@property
18-
def resolution(self) -> Resolution: ...
17+
def _resolution_obj(self) -> Resolution: ...
1918
def _get_to_timestamp_base(self) -> int: ...
19+
def _freqstr(self) -> str: ...
2020

2121
class FreqGroup(Enum):
2222
FR_ANN: int
@@ -33,7 +33,7 @@ class FreqGroup(Enum):
3333
FR_NS: int
3434
FR_UND: int
3535
@staticmethod
36-
def get_freq_group(code: int) -> FreqGroup: ...
36+
def from_period_dtype_code(code: int) -> FreqGroup: ...
3737

3838
class Resolution(Enum):
3939
RESO_NS: int
@@ -49,10 +49,10 @@ class Resolution(Enum):
4949
def __lt__(self, other: Resolution) -> bool: ...
5050
def __ge__(self, other: Resolution) -> bool: ...
5151
@property
52-
def freq_group(self) -> FreqGroup: ...
53-
@property
5452
def attrname(self) -> str: ...
5553
@classmethod
5654
def from_attrname(cls, attrname: str) -> Resolution: ...
5755
@classmethod
58-
def get_reso_from_freq(cls, freq: str) -> Resolution: ...
56+
def get_reso_from_freqstr(cls, freq: str) -> Resolution: ...
57+
@property
58+
def attr_abbrev(self) -> str: ...

0 commit comments

Comments
 (0)