Skip to content

Commit c45e92c

Browse files
authored
CLN: remove get_freq_code (#34674)
* REF: operate on Resolution objs instead of ints * restore * ENH: Resolutions for month/qtr/year * revert comments * remove commented-out * blackify * REF: avoid get_freq_code * CLN: remove get_freq_code * mypy fixup
1 parent 3334c8c commit c45e92c

File tree

19 files changed

+42
-331
lines changed

19 files changed

+42
-331
lines changed

pandas/_libs/tslibs/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"to_offset",
1919
]
2020

21-
from . import dtypes # type: ignore
21+
from . import dtypes
2222
from .conversion import localize_pydatetime
2323
from .nattype import NaT, NaTType, iNaT, is_null_datetimelike, nat_strings
2424
from .np_datetime import OutOfBoundsDatetime

pandas/_libs/tslibs/frequencies.pxd

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
cdef dict attrname_to_abbrevs
22

3-
cpdef get_freq_code(freqstr)
43
cpdef int get_to_timestamp_base(int base)
5-
cpdef str get_freq_str(base, mult=*)

pandas/_libs/tslibs/frequencies.pyx

+5-127
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
cimport numpy as cnp
2-
cnp.import_array()
31

4-
from pandas._libs.tslibs.util cimport is_integer_object
5-
6-
from pandas._libs.tslibs.offsets cimport is_offset_object
7-
from pandas._libs.tslibs.offsets import (
8-
INVALID_FREQ_ERR_MSG,
9-
_dont_uppercase,
10-
_lite_rule_alias,
11-
base_and_stride,
12-
opattern,
13-
)
14-
15-
from .dtypes import FreqGroup, _period_code_map, _reverse_period_code_map
2+
from .dtypes import FreqGroup
163

174
# ---------------------------------------------------------------------
185
# Period codes
@@ -36,131 +23,22 @@ cdef dict attrname_to_abbrevs = _attrname_to_abbrevs
3623

3724
# ----------------------------------------------------------------------
3825

39-
def get_freq_group(freq) -> int:
26+
# TODO: this is now identical to the version in libperiod
27+
def get_freq_group(freq: int) -> int:
4028
"""
4129
Return frequency code group of given frequency str or offset.
4230

4331
Examples
4432
--------
45-
>>> get_freq_group('W-MON')
33+
>>> get_freq_group(4001)
4634
4000
4735

48-
>>> get_freq_group('W-FRI')
36+
>>> get_freq_group(4006)
4937
4000
5038
"""
51-
if is_offset_object(freq):
52-
freq = freq.rule_code
53-
54-
if isinstance(freq, str):
55-
freq = attrname_to_abbrevs.get(freq, freq)
56-
base, mult = get_freq_code(freq)
57-
freq = base
58-
elif isinstance(freq, int):
59-
pass
60-
else:
61-
raise ValueError('input must be str, offset or int')
6239
return (freq // 1000) * 1000
6340

6441

65-
cpdef get_freq_code(freqstr):
66-
"""
67-
Return freq str or tuple to freq code and stride (mult)
68-
69-
Parameters
70-
----------
71-
freqstr : str or tuple
72-
73-
Returns
74-
-------
75-
return : tuple of base frequency code and stride (mult)
76-
77-
Raises
78-
------
79-
TypeError : if passed a tuple witth incorrect types
80-
81-
Examples
82-
--------
83-
>>> get_freq_code('3D')
84-
(6000, 3)
85-
86-
>>> get_freq_code('D')
87-
(6000, 1)
88-
89-
>>> get_freq_code(('D', 3))
90-
(6000, 3)
91-
"""
92-
if is_offset_object(freqstr):
93-
freqstr = (freqstr.rule_code, freqstr.n)
94-
95-
if isinstance(freqstr, tuple):
96-
if is_integer_object(freqstr[0]) and is_integer_object(freqstr[1]):
97-
# e.g., freqstr = (2000, 1)
98-
return freqstr
99-
elif is_integer_object(freqstr[0]):
100-
# Note: passing freqstr[1] below will raise TypeError if that
101-
# is not a str
102-
code = _period_str_to_code(freqstr[1])
103-
stride = freqstr[0]
104-
return code, stride
105-
else:
106-
# e.g., freqstr = ('T', 5)
107-
code = _period_str_to_code(freqstr[0])
108-
stride = freqstr[1]
109-
return code, stride
110-
111-
if is_integer_object(freqstr):
112-
return freqstr, 1
113-
114-
base, stride = base_and_stride(freqstr)
115-
code = _period_str_to_code(base)
116-
117-
return code, stride
118-
119-
120-
cpdef _period_str_to_code(str freqstr):
121-
freqstr = _lite_rule_alias.get(freqstr, freqstr)
122-
123-
if freqstr not in _dont_uppercase:
124-
lower = freqstr.lower()
125-
freqstr = _lite_rule_alias.get(lower, freqstr)
126-
127-
if freqstr not in _dont_uppercase:
128-
freqstr = freqstr.upper()
129-
try:
130-
return _period_code_map[freqstr]
131-
except KeyError:
132-
raise ValueError(INVALID_FREQ_ERR_MSG.format(freqstr))
133-
134-
135-
cpdef str get_freq_str(base, mult=1):
136-
"""
137-
Return the summary string associated with this offset code, possibly
138-
adjusted by a multiplier.
139-
140-
Parameters
141-
----------
142-
base : int (member of FreqGroup)
143-
144-
Returns
145-
-------
146-
freq_str : str
147-
148-
Examples
149-
--------
150-
>>> get_freq_str(1000)
151-
'A-DEC'
152-
153-
>>> get_freq_str(2000, 2)
154-
'2Q-DEC'
155-
156-
>>> get_freq_str("foo")
157-
"""
158-
code = _reverse_period_code_map.get(base)
159-
if mult == 1:
160-
return code
161-
return str(mult) + code
162-
163-
16442
cpdef int get_to_timestamp_base(int base):
16543
"""
16644
Return frequency code group used for base of to_timestamp against

pandas/_libs/tslibs/period.pyx

-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ from pandas._libs.tslibs.dtypes cimport (
7575

7676
from pandas._libs.tslibs.frequencies cimport (
7777
attrname_to_abbrevs,
78-
get_freq_code,
79-
get_freq_str,
8078
get_to_timestamp_base,
8179
)
8280
from pandas._libs.tslibs.parsing cimport get_rule_month

pandas/core/arrays/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
Timestamp,
1212
conversion,
1313
fields,
14-
frequencies as libfrequencies,
1514
iNaT,
15+
offsets as liboffsets,
1616
resolution as libresolution,
1717
timezones,
1818
to_offset,
@@ -1106,7 +1106,7 @@ def to_period(self, freq=None):
11061106

11071107
# https://github.com/pandas-dev/pandas/issues/33358
11081108
if res is None:
1109-
base, stride = libfrequencies.base_and_stride(freq)
1109+
base, stride = liboffsets.base_and_stride(freq)
11101110
res = f"{stride}{base}"
11111111

11121112
freq = res

pandas/core/indexes/period.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def get_loc(self, key, method=None, tolerance=None):
503503

504504
reso = Resolution.from_attrname(reso)
505505
grp = reso.freq_group
506-
freqn = get_freq_group(self.freq)
506+
freqn = get_freq_group(self.dtype.dtype_code)
507507

508508
# _get_string_slice will handle cases where grp < freqn
509509
assert grp >= freqn
@@ -579,7 +579,7 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):
579579
def _validate_partial_date_slice(self, reso: Resolution):
580580
assert isinstance(reso, Resolution), (type(reso), reso)
581581
grp = reso.freq_group
582-
freqn = get_freq_group(self.freq)
582+
freqn = get_freq_group(self.dtype.dtype_code)
583583

584584
if not grp < freqn:
585585
# TODO: we used to also check for

pandas/plotting/_matplotlib/timeseries.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import numpy as np
77

88
from pandas._libs.tslibs import Period, to_offset
9-
from pandas._libs.tslibs.frequencies import FreqGroup, base_and_stride
9+
from pandas._libs.tslibs.frequencies import FreqGroup
10+
from pandas._libs.tslibs.offsets import base_and_stride
1011
from pandas._typing import FrameOrSeriesUnion
1112

1213
from pandas.core.dtypes.generic import (

pandas/tests/indexes/datetimes/test_scalar_compat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88

99
from pandas._libs.tslibs import OutOfBoundsDatetime, to_offset
10+
from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG
1011

1112
import pandas as pd
1213
from pandas import DatetimeIndex, Timestamp, date_range
@@ -118,7 +119,7 @@ def test_round(self, tz_naive_fixture):
118119
tm.assert_index_equal(rng.round(freq="H"), expected_rng)
119120
assert elt.round(freq="H") == expected_elt
120121

121-
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
122+
msg = INVALID_FREQ_ERR_MSG
122123
with pytest.raises(ValueError, match=msg):
123124
rng.round(freq="foo")
124125
with pytest.raises(ValueError, match=msg):

pandas/tests/indexes/datetimes/test_to_period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytz
77

88
from pandas._libs.tslibs.ccalendar import MONTHS
9-
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
9+
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
1010

1111
from pandas import (
1212
DatetimeIndex,

pandas/tests/indexes/timedeltas/test_scalar_compat.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import numpy as np
66
import pytest
77

8+
from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG
9+
810
import pandas as pd
911
from pandas import Index, Series, Timedelta, TimedeltaIndex, timedelta_range
1012
import pandas._testing as tm
@@ -58,7 +60,7 @@ def test_tdi_round(self):
5860
tm.assert_index_equal(td.round(freq="H"), expected_rng)
5961
assert elt.round(freq="H") == expected_elt
6062

61-
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
63+
msg = INVALID_FREQ_ERR_MSG
6264
with pytest.raises(ValueError, match=msg):
6365
td.round(freq="foo")
6466
with pytest.raises(ValueError, match=msg):

pandas/tests/scalar/period/test_asfreq.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

3-
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG, _period_code_map
3+
from pandas._libs.tslibs.dtypes import _period_code_map
4+
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
45
from pandas.errors import OutOfBoundsDatetime
56

67
from pandas import Period, Timestamp, offsets

pandas/tests/scalar/period/test_period.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
from pandas._libs.tslibs import iNaT, period as libperiod
88
from pandas._libs.tslibs.ccalendar import DAYS, MONTHS
9-
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
109
from pandas._libs.tslibs.parsing import DateParseError
11-
from pandas._libs.tslibs.period import IncompatibleFrequency
10+
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG, IncompatibleFrequency
1211
from pandas._libs.tslibs.timezones import dateutil_gettz, maybe_get_tz
1312
from pandas.compat.numpy import np_datetime64_compat
1413

pandas/tests/scalar/timestamp/test_unary_ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pytz import utc
77

88
from pandas._libs.tslibs import NaT, Timestamp, conversion, to_offset
9-
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
9+
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
1010
import pandas.util._test_decorators as td
1111

1212
import pandas._testing as tm

0 commit comments

Comments
 (0)