Skip to content

CLN: remove Resolution.get_attrname_from_abbrev #34524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 7 additions & 30 deletions pandas/_libs/tslibs/resolution.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ _reso_str_map = {

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

# factor to multiply a value by to convert it to the next finer grained
# resolution
_reso_mult_map = {
RESO_NS: None,
RESO_US: 1000,
RESO_MS: 1000,
RESO_SEC: 1000,
RESO_MIN: 60,
RESO_HR: 60,
RESO_DAY: 24,
}

# ----------------------------------------------------------------------


Expand Down Expand Up @@ -145,17 +133,17 @@ class Resolution(Enum):
def __ge__(self, other):
return self.value >= other.value

@classmethod
def get_str(cls, reso: "Resolution") -> str:
@property
def attrname(self) -> str:
"""
Return resolution str against resolution code.
Return datetime attribute name corresponding to this Resolution.

Examples
--------
>>> Resolution.get_str(Resolution.RESO_SEC)
>>> Resolution.RESO_SEC.attrname
'second'
"""
return _reso_str_map[reso.value]
return _reso_str_map[self.value]

@classmethod
def from_attrname(cls, attrname: str) -> "Resolution":
Expand All @@ -172,18 +160,6 @@ class Resolution(Enum):
"""
return cls(_str_reso_map[attrname])

@classmethod
def get_attrname_from_abbrev(cls, freq: str) -> str:
"""
Return resolution str against frequency str.

Examples
--------
>>> Resolution.get_attrname_from_abbrev('H')
'hour'
"""
return _abbrev_to_attrnames[freq]

@classmethod
def get_reso_from_freq(cls, freq: str) -> "Resolution":
"""
Expand All @@ -199,7 +175,8 @@ class Resolution(Enum):
>>> Resolution.get_reso_from_freq('H') == Resolution.RESO_HR
True
"""
return cls.from_attrname(cls.get_attrname_from_abbrev(freq))
attr_name = _abbrev_to_attrnames[freq]
return cls.from_attrname(attr_name)


# ----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ def resolution(self) -> str:
# somewhere in the past it was decided we default to day
return "day"
# otherwise we fall through and will raise
return Resolution.get_str(self._resolution_obj)
return self._resolution_obj.attrname # type: ignore

@classmethod
def _validate_frequency(cls, index, freq, **kwargs):
Expand Down
18 changes: 8 additions & 10 deletions pandas/tests/tseries/frequencies/test_freq_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ def test_get_to_timestamp_base(freqstr, exp_freqstr):
@pytest.mark.parametrize(
"freqstr,expected",
[
("A", "year"),
("Q", "quarter"),
("M", "month"),
("D", "day"),
("H", "hour"),
("T", "minute"),
Expand All @@ -106,19 +103,20 @@ def test_get_to_timestamp_base(freqstr, exp_freqstr):
],
)
def test_get_attrname_from_abbrev(freqstr, expected):
assert _reso.get_attrname_from_abbrev(freqstr) == expected
assert _reso.get_reso_from_freq(freqstr).attrname == expected


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


@pytest.mark.parametrize("freq", ["D", "H", "T", "S", "L", "U"])
@pytest.mark.parametrize("freq", ["D", "H", "T", "S", "L", "U", "N"])
def test_get_freq_roundtrip2(freq):
obj = _reso.get_reso_from_freq(freq)
result = _attrname_to_abbrevs[_reso.get_str(obj)]
result = _attrname_to_abbrevs[obj.attrname]
assert freq == result


Expand Down