Skip to content

Commit 4743cc9

Browse files
authored
CLN: remove Resolution.get_stride_from_decimal; rename (#34515)
1 parent 2428cdd commit 4743cc9

File tree

4 files changed

+15
-61
lines changed

4 files changed

+15
-61
lines changed

pandas/_libs/tslibs/offsets.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ cdef class Tick(SingleConstructorOffset):
762762
return Micro(self.n * 1000)
763763
if type(self) is Micro:
764764
return Nano(self.n * 1000)
765-
raise NotImplementedError(type(self))
765+
raise ValueError("Could not convert to integer offset at any resolution")
766766

767767
# --------------------------------------------------------------------
768768

pandas/_libs/tslibs/resolution.pyx

+5-55
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ cdef:
2727
int RESO_HR = 5
2828
int RESO_DAY = 6
2929

30-
reso_str_bump_map = {
31-
"D": "H",
32-
"H": "T",
33-
"T": "S",
34-
"S": "L",
35-
"L": "U",
36-
"U": "N",
37-
"N": None,
38-
}
39-
4030
_abbrev_to_attrnames = {v: k for k, v in attrname_to_abbrevs.items()}
4131

4232
_reso_str_map = {
@@ -168,19 +158,19 @@ class Resolution(Enum):
168158
return _reso_str_map[reso.value]
169159

170160
@classmethod
171-
def get_reso(cls, resostr: str) -> "Resolution":
161+
def from_attrname(cls, attrname: str) -> "Resolution":
172162
"""
173163
Return resolution str against resolution code.
174164

175165
Examples
176166
--------
177-
>>> Resolution.get_reso('second')
167+
>>> Resolution.from_attrname('second')
178168
2
179169

180-
>>> Resolution.get_reso('second') == Resolution.RESO_SEC
170+
>>> Resolution.from_attrname('second') == Resolution.RESO_SEC
181171
True
182172
"""
183-
return cls(_str_reso_map[resostr])
173+
return cls(_str_reso_map[attrname])
184174

185175
@classmethod
186176
def get_attrname_from_abbrev(cls, freq: str) -> str:
@@ -209,47 +199,7 @@ class Resolution(Enum):
209199
>>> Resolution.get_reso_from_freq('H') == Resolution.RESO_HR
210200
True
211201
"""
212-
return cls.get_reso(cls.get_attrname_from_abbrev(freq))
213-
214-
@classmethod
215-
def get_stride_from_decimal(cls, value: float, freq: str):
216-
"""
217-
Convert freq with decimal stride into a higher freq with integer stride
218-
219-
Parameters
220-
----------
221-
value : float
222-
freq : str
223-
Frequency string
224-
225-
Raises
226-
------
227-
ValueError
228-
If the float cannot be converted to an integer at any resolution.
229-
230-
Examples
231-
--------
232-
>>> Resolution.get_stride_from_decimal(1.5, 'T')
233-
(90, 'S')
234-
235-
>>> Resolution.get_stride_from_decimal(1.04, 'H')
236-
(3744, 'S')
237-
238-
>>> Resolution.get_stride_from_decimal(1, 'D')
239-
(1, 'D')
240-
"""
241-
if np.isclose(value % 1, 0):
242-
return int(value), freq
243-
else:
244-
start_reso = cls.get_reso_from_freq(freq)
245-
if start_reso.value == 0:
246-
raise ValueError(
247-
"Could not convert to integer offset at any resolution"
248-
)
249-
250-
next_value = _reso_mult_map[start_reso.value] * value
251-
next_name = reso_str_bump_map[freq]
252-
return cls.get_stride_from_decimal(next_value, next_name)
202+
return cls.from_attrname(cls.get_attrname_from_abbrev(freq))
253203

254204

255205
# ----------------------------------------------------------------------

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def _validate_partial_date_slice(self, reso: str):
525525
if (
526526
self.is_monotonic
527527
and reso in ["day", "hour", "minute", "second"]
528-
and self._resolution >= libresolution.Resolution.get_reso(reso)
528+
and self._resolution >= libresolution.Resolution.from_attrname(reso)
529529
):
530530
# These resolution/monotonicity validations came from GH3931,
531531
# GH3452 and GH2369.

pandas/tests/tseries/frequencies/test_freq_code.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
)
1111
from pandas._libs.tslibs.resolution import Resolution as _reso
1212

13+
from pandas.tseries.frequencies import to_offset
1314
import pandas.tseries.offsets as offsets
1415

1516

@@ -116,7 +117,8 @@ def test_get_freq_roundtrip(freq):
116117

117118
@pytest.mark.parametrize("freq", ["D", "H", "T", "S", "L", "U"])
118119
def test_get_freq_roundtrip2(freq):
119-
result = _attrname_to_abbrevs[_reso.get_str(_reso.get_reso_from_freq(freq))]
120+
obj = _reso.get_reso_from_freq(freq)
121+
result = _attrname_to_abbrevs[_reso.get_str(obj)]
120122
assert freq == result
121123

122124

@@ -133,7 +135,9 @@ def test_get_freq_roundtrip2(freq):
133135
)
134136
def test_resolution_bumping(args, expected):
135137
# see gh-14378
136-
assert _reso.get_stride_from_decimal(*args) == expected
138+
off = to_offset(str(args[0]) + args[1])
139+
assert off.n == expected[0]
140+
assert off._prefix == expected[1]
137141

138142

139143
@pytest.mark.parametrize(
@@ -145,10 +149,10 @@ def test_resolution_bumping(args, expected):
145149
],
146150
)
147151
def test_cat(args):
148-
msg = "Could not convert to integer offset at any resolution"
152+
msg = "Invalid frequency"
149153

150154
with pytest.raises(ValueError, match=msg):
151-
_reso.get_stride_from_decimal(*args)
155+
to_offset(str(args[0]) + args[1])
152156

153157

154158
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)