Skip to content

Commit d8b5bb3

Browse files
authored
CLN: remove libfrequencies.get_freq_group (#34701)
1 parent 7630f38 commit d8b5bb3

File tree

9 files changed

+40
-52
lines changed

9 files changed

+40
-52
lines changed

pandas/_libs/tslibs/dtypes.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cdef dict attrname_to_abbrevs
12

23
cdef enum c_FreqGroup:
34
# Mirrors FreqGroup in the .pxy file

pandas/_libs/tslibs/dtypes.pyx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ cdef class PeriodDtypeBase:
2121
return False
2222
return self.dtype_code == other.dtype_code
2323

24+
@property
25+
def freq_group(self) -> int:
26+
# See also: libperiod.get_freq_group
27+
return (self.dtype_code // 1000) * 1000
28+
2429
@property
2530
def date_offset(self):
2631
"""
@@ -108,6 +113,22 @@ _period_code_map.update({
108113
})
109114

110115

116+
# Map attribute-name resolutions to resolution abbreviations
117+
_attrname_to_abbrevs = {
118+
"year": "A",
119+
"quarter": "Q",
120+
"month": "M",
121+
"day": "D",
122+
"hour": "H",
123+
"minute": "T",
124+
"second": "S",
125+
"millisecond": "L",
126+
"microsecond": "U",
127+
"nanosecond": "N",
128+
}
129+
cdef dict attrname_to_abbrevs = _attrname_to_abbrevs
130+
131+
111132
class FreqGroup:
112133
# Mirrors c_FreqGroup in the .pxd file
113134
FR_ANN = 1000
@@ -123,3 +144,8 @@ class FreqGroup:
123144
FR_US = 11000
124145
FR_NS = 12000
125146
FR_UND = -10000 # undefined
147+
148+
@staticmethod
149+
def get_freq_group(code: int) -> int:
150+
# See also: PeriodDtypeBase.freq_group
151+
return (code // 1000) * 1000

pandas/_libs/tslibs/frequencies.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
cdef dict attrname_to_abbrevs
2-
31
cpdef int get_to_timestamp_base(int base)

pandas/_libs/tslibs/frequencies.pyx

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,8 @@
11

22
from .dtypes import FreqGroup
33

4-
# ---------------------------------------------------------------------
5-
# Period codes
6-
7-
8-
# Map attribute-name resolutions to resolution abbreviations
9-
_attrname_to_abbrevs = {
10-
"year": "A",
11-
"quarter": "Q",
12-
"month": "M",
13-
"day": "D",
14-
"hour": "H",
15-
"minute": "T",
16-
"second": "S",
17-
"millisecond": "L",
18-
"microsecond": "U",
19-
"nanosecond": "N",
20-
}
21-
cdef dict attrname_to_abbrevs = _attrname_to_abbrevs
22-
23-
244
# ----------------------------------------------------------------------
255

26-
# TODO: this is now identical to the version in libperiod
27-
def get_freq_group(freq: int) -> int:
28-
"""
29-
Return frequency code group of given frequency str or offset.
30-
31-
Examples
32-
--------
33-
>>> get_freq_group(4001)
34-
4000
35-
36-
>>> get_freq_group(4006)
37-
4000
38-
"""
39-
return (freq // 1000) * 1000
40-
416

427
cpdef int get_to_timestamp_base(int base):
438
"""

pandas/_libs/tslibs/period.pyx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,10 @@ from pandas._libs.tslibs.dtypes cimport (
7171
FR_MS,
7272
FR_US,
7373
FR_NS,
74-
)
75-
76-
from pandas._libs.tslibs.frequencies cimport (
7774
attrname_to_abbrevs,
78-
get_to_timestamp_base,
7975
)
76+
77+
from pandas._libs.tslibs.frequencies cimport get_to_timestamp_base
8078
from pandas._libs.tslibs.parsing cimport get_rule_month
8179
from pandas._libs.tslibs.parsing import parse_time_string
8280
from pandas._libs.tslibs.nattype cimport (

pandas/_libs/tslibs/resolution.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ from numpy cimport ndarray, int64_t, int32_t
55

66
from pandas._libs.tslibs.util cimport get_nat
77

8+
from pandas._libs.tslibs.dtypes cimport attrname_to_abbrevs
89
from pandas._libs.tslibs.np_datetime cimport (
910
npy_datetimestruct, dt64_to_dtstruct)
10-
from pandas._libs.tslibs.frequencies cimport attrname_to_abbrevs
1111
from pandas._libs.tslibs.frequencies import FreqGroup
1212
from pandas._libs.tslibs.timezones cimport (
1313
is_utc, is_tzlocal, maybe_get_tz, get_dst_info)

pandas/core/indexes/period.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from pandas._libs import index as libindex
77
from pandas._libs.lib import no_default
88
from pandas._libs.tslibs import Period, Resolution
9-
from pandas._libs.tslibs.frequencies import get_freq_group
109
from pandas._libs.tslibs.parsing import DateParseError, parse_time_string
1110
from pandas._typing import DtypeObj, Label
1211
from pandas.util._decorators import Appender, cache_readonly, doc
@@ -510,7 +509,7 @@ def get_loc(self, key, method=None, tolerance=None):
510509

511510
reso = Resolution.from_attrname(reso)
512511
grp = reso.freq_group
513-
freqn = get_freq_group(self.dtype.dtype_code)
512+
freqn = self.dtype.freq_group
514513

515514
# _get_string_slice will handle cases where grp < freqn
516515
assert grp >= freqn
@@ -586,7 +585,7 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):
586585
def _validate_partial_date_slice(self, reso: Resolution):
587586
assert isinstance(reso, Resolution), (type(reso), reso)
588587
grp = reso.freq_group
589-
freqn = get_freq_group(self.dtype.dtype_code)
588+
freqn = self.dtype.freq_group
590589

591590
if not grp < freqn:
592591
# TODO: we used to also check for

pandas/plotting/_matplotlib/converter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import matplotlib.units as units
1111
import numpy as np
1212

13-
from pandas._libs import lib, tslibs
14-
from pandas._libs.tslibs import to_offset
15-
from pandas._libs.tslibs.frequencies import FreqGroup, get_freq_group
13+
from pandas._libs import lib
14+
from pandas._libs.tslibs import Timestamp, to_offset
15+
from pandas._libs.tslibs.dtypes import FreqGroup
1616
from pandas._libs.tslibs.offsets import BaseOffset
1717

1818
from pandas.core.dtypes.common import (
@@ -45,7 +45,7 @@
4545

4646
def get_pairs():
4747
pairs = [
48-
(tslibs.Timestamp, DatetimeConverter),
48+
(Timestamp, DatetimeConverter),
4949
(Period, PeriodConverter),
5050
(pydt.datetime, DatetimeConverter),
5151
(pydt.date, DatetimeConverter),
@@ -281,7 +281,7 @@ def try_parse(values):
281281
if isinstance(values, (datetime, pydt.date)):
282282
return _dt_to_float_ordinal(values)
283283
elif isinstance(values, np.datetime64):
284-
return _dt_to_float_ordinal(tslibs.Timestamp(values))
284+
return _dt_to_float_ordinal(Timestamp(values))
285285
elif isinstance(values, pydt.time):
286286
return dates.date2num(values)
287287
elif is_integer(values) or is_float(values):
@@ -553,7 +553,7 @@ def _daily_finder(vmin, vmax, freq: BaseOffset):
553553
elif dtype_code == FreqGroup.FR_DAY:
554554
periodsperyear = 365
555555
periodspermonth = 28
556-
elif get_freq_group(dtype_code) == FreqGroup.FR_WK:
556+
elif FreqGroup.get_freq_group(dtype_code) == FreqGroup.FR_WK:
557557
periodsperyear = 52
558558
periodspermonth = 3
559559
else: # pragma: no cover

pandas/tests/tseries/frequencies/test_freq_code.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import pytest
22

33
from pandas._libs.tslibs import Resolution, to_offset
4-
from pandas._libs.tslibs.frequencies import _attrname_to_abbrevs, get_to_timestamp_base
4+
from pandas._libs.tslibs.dtypes import _attrname_to_abbrevs
5+
from pandas._libs.tslibs.frequencies import get_to_timestamp_base
56

67

78
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)