Skip to content

CLN: remove get_freq_code #34674

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 19 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b542f7d
REF: operate on Resolution objs instead of ints
jbrockmendel Jun 2, 2020
82a166d
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jun 2, 2020
ffe0d95
restore
jbrockmendel Jun 2, 2020
ffd325b
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jun 3, 2020
4170e69
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jun 4, 2020
7f8bee4
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jun 4, 2020
532a3c8
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jun 4, 2020
cf71c6b
ENH: Resolutions for month/qtr/year
jbrockmendel Jun 5, 2020
a04be73
revert comments
jbrockmendel Jun 5, 2020
c7791b9
remove commented-out
jbrockmendel Jun 5, 2020
b23b11f
blackify
jbrockmendel Jun 5, 2020
1475b0e
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jun 8, 2020
0217333
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jun 9, 2020
6d93dbe
REF: avoid get_freq_code
jbrockmendel Jun 8, 2020
3431c0f
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel Jun 9, 2020
b525a18
CLN: remove get_freq_code
jbrockmendel Jun 9, 2020
8bd8db9
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel Jun 9, 2020
fee5c39
mypy fixup
jbrockmendel Jun 9, 2020
af3a5a8
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel Jun 9, 2020
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
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"to_offset",
]

from . import dtypes # type: ignore
from . import dtypes
from .conversion import localize_pydatetime
from .nattype import NaT, NaTType, iNaT, is_null_datetimelike, nat_strings
from .np_datetime import OutOfBoundsDatetime
Expand Down
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/frequencies.pxd
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
cdef dict attrname_to_abbrevs

cpdef get_freq_code(freqstr)
cpdef int get_to_timestamp_base(int base)
cpdef str get_freq_str(base, mult=*)
132 changes: 5 additions & 127 deletions pandas/_libs/tslibs/frequencies.pyx
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
cimport numpy as cnp
cnp.import_array()

from pandas._libs.tslibs.util cimport is_integer_object

from pandas._libs.tslibs.offsets cimport is_offset_object
from pandas._libs.tslibs.offsets import (
INVALID_FREQ_ERR_MSG,
_dont_uppercase,
_lite_rule_alias,
base_and_stride,
opattern,
)

from .dtypes import FreqGroup, _period_code_map, _reverse_period_code_map
from .dtypes import FreqGroup

# ---------------------------------------------------------------------
# Period codes
Expand All @@ -36,131 +23,22 @@ cdef dict attrname_to_abbrevs = _attrname_to_abbrevs

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

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

Examples
--------
>>> get_freq_group('W-MON')
>>> get_freq_group(4001)
4000

>>> get_freq_group('W-FRI')
>>> get_freq_group(4006)
4000
"""
if is_offset_object(freq):
freq = freq.rule_code

if isinstance(freq, str):
freq = attrname_to_abbrevs.get(freq, freq)
base, mult = get_freq_code(freq)
freq = base
elif isinstance(freq, int):
pass
else:
raise ValueError('input must be str, offset or int')
return (freq // 1000) * 1000


cpdef get_freq_code(freqstr):
"""
Return freq str or tuple to freq code and stride (mult)

Parameters
----------
freqstr : str or tuple

Returns
-------
return : tuple of base frequency code and stride (mult)

Raises
------
TypeError : if passed a tuple witth incorrect types

Examples
--------
>>> get_freq_code('3D')
(6000, 3)

>>> get_freq_code('D')
(6000, 1)

>>> get_freq_code(('D', 3))
(6000, 3)
"""
if is_offset_object(freqstr):
freqstr = (freqstr.rule_code, freqstr.n)

if isinstance(freqstr, tuple):
if is_integer_object(freqstr[0]) and is_integer_object(freqstr[1]):
# e.g., freqstr = (2000, 1)
return freqstr
elif is_integer_object(freqstr[0]):
# Note: passing freqstr[1] below will raise TypeError if that
# is not a str
code = _period_str_to_code(freqstr[1])
stride = freqstr[0]
return code, stride
else:
# e.g., freqstr = ('T', 5)
code = _period_str_to_code(freqstr[0])
stride = freqstr[1]
return code, stride

if is_integer_object(freqstr):
return freqstr, 1

base, stride = base_and_stride(freqstr)
code = _period_str_to_code(base)

return code, stride


cpdef _period_str_to_code(str freqstr):
freqstr = _lite_rule_alias.get(freqstr, freqstr)

if freqstr not in _dont_uppercase:
lower = freqstr.lower()
freqstr = _lite_rule_alias.get(lower, freqstr)

if freqstr not in _dont_uppercase:
freqstr = freqstr.upper()
try:
return _period_code_map[freqstr]
except KeyError:
raise ValueError(INVALID_FREQ_ERR_MSG.format(freqstr))


cpdef str get_freq_str(base, mult=1):
"""
Return the summary string associated with this offset code, possibly
adjusted by a multiplier.

Parameters
----------
base : int (member of FreqGroup)

Returns
-------
freq_str : str

Examples
--------
>>> get_freq_str(1000)
'A-DEC'

>>> get_freq_str(2000, 2)
'2Q-DEC'

>>> get_freq_str("foo")
"""
code = _reverse_period_code_map.get(base)
if mult == 1:
return code
return str(mult) + code


cpdef int get_to_timestamp_base(int base):
"""
Return frequency code group used for base of to_timestamp against
Expand Down
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ from pandas._libs.tslibs.dtypes cimport (

from pandas._libs.tslibs.frequencies cimport (
attrname_to_abbrevs,
get_freq_code,
get_freq_str,
get_to_timestamp_base,
)
from pandas._libs.tslibs.parsing cimport get_rule_month
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
Timestamp,
conversion,
fields,
frequencies as libfrequencies,
iNaT,
offsets as liboffsets,
resolution as libresolution,
timezones,
to_offset,
Expand Down Expand Up @@ -1106,7 +1106,7 @@ def to_period(self, freq=None):

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

freq = res
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def get_loc(self, key, method=None, tolerance=None):

reso = Resolution.from_attrname(reso)
grp = reso.freq_group
freqn = get_freq_group(self.freq)
freqn = get_freq_group(self.dtype.dtype_code)

# _get_string_slice will handle cases where grp < freqn
assert grp >= freqn
Expand Down Expand Up @@ -579,7 +579,7 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):
def _validate_partial_date_slice(self, reso: Resolution):
assert isinstance(reso, Resolution), (type(reso), reso)
grp = reso.freq_group
freqn = get_freq_group(self.freq)
freqn = get_freq_group(self.dtype.dtype_code)

if not grp < freqn:
# TODO: we used to also check for
Expand Down
3 changes: 2 additions & 1 deletion pandas/plotting/_matplotlib/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import numpy as np

from pandas._libs.tslibs import Period, to_offset
from pandas._libs.tslibs.frequencies import FreqGroup, base_and_stride
from pandas._libs.tslibs.frequencies import FreqGroup
from pandas._libs.tslibs.offsets import base_and_stride
from pandas._typing import FrameOrSeriesUnion

from pandas.core.dtypes.generic import (
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimes/test_scalar_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from pandas._libs.tslibs import OutOfBoundsDatetime, to_offset
from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG

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

msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
msg = INVALID_FREQ_ERR_MSG
with pytest.raises(ValueError, match=msg):
rng.round(freq="foo")
with pytest.raises(ValueError, match=msg):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_to_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytz

from pandas._libs.tslibs.ccalendar import MONTHS
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG

from pandas import (
DatetimeIndex,
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexes/timedeltas/test_scalar_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
import pytest

from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG

import pandas as pd
from pandas import Index, Series, Timedelta, TimedeltaIndex, timedelta_range
import pandas._testing as tm
Expand Down Expand Up @@ -58,7 +60,7 @@ def test_tdi_round(self):
tm.assert_index_equal(td.round(freq="H"), expected_rng)
assert elt.round(freq="H") == expected_elt

msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
msg = INVALID_FREQ_ERR_MSG
with pytest.raises(ValueError, match=msg):
td.round(freq="foo")
with pytest.raises(ValueError, match=msg):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/scalar/period/test_asfreq.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG, _period_code_map
from pandas._libs.tslibs.dtypes import _period_code_map
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
from pandas.errors import OutOfBoundsDatetime

from pandas import Period, Timestamp, offsets
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

from pandas._libs.tslibs import iNaT, period as libperiod
from pandas._libs.tslibs.ccalendar import DAYS, MONTHS
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
from pandas._libs.tslibs.parsing import DateParseError
from pandas._libs.tslibs.period import IncompatibleFrequency
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG, IncompatibleFrequency
from pandas._libs.tslibs.timezones import dateutil_gettz, maybe_get_tz
from pandas.compat.numpy import np_datetime64_compat

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/scalar/timestamp/test_unary_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pytz import utc

from pandas._libs.tslibs import NaT, Timestamp, conversion, to_offset
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
import pandas.util._test_decorators as td

import pandas._testing as tm
Expand Down
Loading