Skip to content

Commit 8c66220

Browse files
authored
CLN: remove Resolution.get_attrname_from_abbrev (pandas-dev#34524)
1 parent 0dfe989 commit 8c66220

File tree

3 files changed

+16
-41
lines changed

3 files changed

+16
-41
lines changed

pandas/_libs/tslibs/resolution.pyx

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@ _reso_str_map = {
4141

4242
_str_reso_map = {v: k for k, v in _reso_str_map.items()}
4343

44-
# factor to multiply a value by to convert it to the next finer grained
45-
# resolution
46-
_reso_mult_map = {
47-
RESO_NS: None,
48-
RESO_US: 1000,
49-
RESO_MS: 1000,
50-
RESO_SEC: 1000,
51-
RESO_MIN: 60,
52-
RESO_HR: 60,
53-
RESO_DAY: 24,
54-
}
55-
5644
# ----------------------------------------------------------------------
5745

5846

@@ -145,17 +133,17 @@ class Resolution(Enum):
145133
def __ge__(self, other):
146134
return self.value >= other.value
147135

148-
@classmethod
149-
def get_str(cls, reso: "Resolution") -> str:
136+
@property
137+
def attrname(self) -> str:
150138
"""
151-
Return resolution str against resolution code.
139+
Return datetime attribute name corresponding to this Resolution.
152140

153141
Examples
154142
--------
155-
>>> Resolution.get_str(Resolution.RESO_SEC)
143+
>>> Resolution.RESO_SEC.attrname
156144
'second'
157145
"""
158-
return _reso_str_map[reso.value]
146+
return _reso_str_map[self.value]
159147

160148
@classmethod
161149
def from_attrname(cls, attrname: str) -> "Resolution":
@@ -172,18 +160,6 @@ class Resolution(Enum):
172160
"""
173161
return cls(_str_reso_map[attrname])
174162

175-
@classmethod
176-
def get_attrname_from_abbrev(cls, freq: str) -> str:
177-
"""
178-
Return resolution str against frequency str.
179-
180-
Examples
181-
--------
182-
>>> Resolution.get_attrname_from_abbrev('H')
183-
'hour'
184-
"""
185-
return _abbrev_to_attrnames[freq]
186-
187163
@classmethod
188164
def get_reso_from_freq(cls, freq: str) -> "Resolution":
189165
"""
@@ -199,7 +175,8 @@ class Resolution(Enum):
199175
>>> Resolution.get_reso_from_freq('H') == Resolution.RESO_HR
200176
True
201177
"""
202-
return cls.from_attrname(cls.get_attrname_from_abbrev(freq))
178+
attr_name = _abbrev_to_attrnames[freq]
179+
return cls.from_attrname(attr_name)
203180

204181

205182
# ----------------------------------------------------------------------

pandas/core/arrays/datetimelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ def resolution(self) -> str:
11261126
# somewhere in the past it was decided we default to day
11271127
return "day"
11281128
# otherwise we fall through and will raise
1129-
return Resolution.get_str(self._resolution_obj)
1129+
return self._resolution_obj.attrname # type: ignore
11301130

11311131
@classmethod
11321132
def _validate_frequency(cls, index, freq, **kwargs):

pandas/tests/tseries/frequencies/test_freq_code.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ def test_get_to_timestamp_base(freqstr, exp_freqstr):
9393
@pytest.mark.parametrize(
9494
"freqstr,expected",
9595
[
96-
("A", "year"),
97-
("Q", "quarter"),
98-
("M", "month"),
9996
("D", "day"),
10097
("H", "hour"),
10198
("T", "minute"),
@@ -106,19 +103,20 @@ def test_get_to_timestamp_base(freqstr, exp_freqstr):
106103
],
107104
)
108105
def test_get_attrname_from_abbrev(freqstr, expected):
109-
assert _reso.get_attrname_from_abbrev(freqstr) == expected
106+
assert _reso.get_reso_from_freq(freqstr).attrname == expected
110107

111108

112-
@pytest.mark.parametrize("freq", ["A", "Q", "M", "D", "H", "T", "S", "L", "U", "N"])
113-
def test_get_freq_roundtrip(freq):
114-
result = _attrname_to_abbrevs[_reso.get_attrname_from_abbrev(freq)]
115-
assert freq == result
109+
@pytest.mark.parametrize("freq", ["A", "Q", "M"])
110+
def test_get_freq_unsupported_(freq):
111+
# Lowest-frequency resolution is for Day
112+
with pytest.raises(KeyError, match=freq.lower()):
113+
_reso.get_reso_from_freq(freq)
116114

117115

118-
@pytest.mark.parametrize("freq", ["D", "H", "T", "S", "L", "U"])
116+
@pytest.mark.parametrize("freq", ["D", "H", "T", "S", "L", "U", "N"])
119117
def test_get_freq_roundtrip2(freq):
120118
obj = _reso.get_reso_from_freq(freq)
121-
result = _attrname_to_abbrevs[_reso.get_str(obj)]
119+
result = _attrname_to_abbrevs[obj.attrname]
122120
assert freq == result
123121

124122

0 commit comments

Comments
 (0)