Skip to content

CLN: remove libfrequencies.get_freq_group #34701

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 7 commits into from
Jun 14, 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
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/dtypes.pxd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cdef dict attrname_to_abbrevs

cdef enum c_FreqGroup:
# Mirrors FreqGroup in the .pxy file
Expand Down
26 changes: 26 additions & 0 deletions pandas/_libs/tslibs/dtypes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ cdef class PeriodDtypeBase:
return False
return self.dtype_code == other.dtype_code

@property
def freq_group(self) -> int:
# See also: libperiod.get_freq_group
return (self.dtype_code // 1000) * 1000

@property
def date_offset(self):
"""
Expand Down Expand Up @@ -108,6 +113,22 @@ _period_code_map.update({
})


# Map attribute-name resolutions to resolution abbreviations
_attrname_to_abbrevs = {
"year": "A",
"quarter": "Q",
"month": "M",
"day": "D",
"hour": "H",
"minute": "T",
"second": "S",
"millisecond": "L",
"microsecond": "U",
"nanosecond": "N",
}
cdef dict attrname_to_abbrevs = _attrname_to_abbrevs


class FreqGroup:
# Mirrors c_FreqGroup in the .pxd file
FR_ANN = 1000
Expand All @@ -123,3 +144,8 @@ class FreqGroup:
FR_US = 11000
FR_NS = 12000
FR_UND = -10000 # undefined

@staticmethod
def get_freq_group(code: int) -> int:
# See also: PeriodDtypeBase.freq_group
return (code // 1000) * 1000
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/frequencies.pxd
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
cdef dict attrname_to_abbrevs

cpdef int get_to_timestamp_base(int base)
35 changes: 0 additions & 35 deletions pandas/_libs/tslibs/frequencies.pyx
Original file line number Diff line number Diff line change
@@ -1,43 +1,8 @@

from .dtypes import FreqGroup

# ---------------------------------------------------------------------
# Period codes


# Map attribute-name resolutions to resolution abbreviations
_attrname_to_abbrevs = {
"year": "A",
"quarter": "Q",
"month": "M",
"day": "D",
"hour": "H",
"minute": "T",
"second": "S",
"millisecond": "L",
"microsecond": "U",
"nanosecond": "N",
}
cdef dict attrname_to_abbrevs = _attrname_to_abbrevs


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

# 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(4001)
4000

>>> get_freq_group(4006)
4000
"""
return (freq // 1000) * 1000


cpdef int get_to_timestamp_base(int base):
"""
Expand Down
6 changes: 2 additions & 4 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,10 @@ from pandas._libs.tslibs.dtypes cimport (
FR_MS,
FR_US,
FR_NS,
)

from pandas._libs.tslibs.frequencies cimport (
attrname_to_abbrevs,
get_to_timestamp_base,
)

from pandas._libs.tslibs.frequencies cimport get_to_timestamp_base
from pandas._libs.tslibs.parsing cimport get_rule_month
from pandas._libs.tslibs.parsing import parse_time_string
from pandas._libs.tslibs.nattype cimport (
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/resolution.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ from numpy cimport ndarray, int64_t, int32_t

from pandas._libs.tslibs.util cimport get_nat

from pandas._libs.tslibs.dtypes cimport attrname_to_abbrevs
from pandas._libs.tslibs.np_datetime cimport (
npy_datetimestruct, dt64_to_dtstruct)
from pandas._libs.tslibs.frequencies cimport attrname_to_abbrevs
from pandas._libs.tslibs.frequencies import FreqGroup
from pandas._libs.tslibs.timezones cimport (
is_utc, is_tzlocal, maybe_get_tz, get_dst_info)
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pandas._libs import index as libindex
from pandas._libs.lib import no_default
from pandas._libs.tslibs import Period, Resolution
from pandas._libs.tslibs.frequencies import get_freq_group
from pandas._libs.tslibs.parsing import DateParseError, parse_time_string
from pandas._typing import DtypeObj, Label
from pandas.util._decorators import Appender, cache_readonly, doc
Expand Down Expand Up @@ -510,7 +509,7 @@ def get_loc(self, key, method=None, tolerance=None):

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

# _get_string_slice will handle cases where grp < freqn
assert grp >= freqn
Expand Down Expand Up @@ -586,7 +585,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.dtype.dtype_code)
freqn = self.dtype.freq_group

if not grp < freqn:
# TODO: we used to also check for
Expand Down
12 changes: 6 additions & 6 deletions pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import matplotlib.units as units
import numpy as np

from pandas._libs import lib, tslibs
from pandas._libs.tslibs import to_offset
from pandas._libs.tslibs.frequencies import FreqGroup, get_freq_group
from pandas._libs import lib
from pandas._libs.tslibs import Timestamp, to_offset
from pandas._libs.tslibs.dtypes import FreqGroup
from pandas._libs.tslibs.offsets import BaseOffset

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -45,7 +45,7 @@

def get_pairs():
pairs = [
(tslibs.Timestamp, DatetimeConverter),
(Timestamp, DatetimeConverter),
(Period, PeriodConverter),
(pydt.datetime, DatetimeConverter),
(pydt.date, DatetimeConverter),
Expand Down Expand Up @@ -281,7 +281,7 @@ def try_parse(values):
if isinstance(values, (datetime, pydt.date)):
return _dt_to_float_ordinal(values)
elif isinstance(values, np.datetime64):
return _dt_to_float_ordinal(tslibs.Timestamp(values))
return _dt_to_float_ordinal(Timestamp(values))
elif isinstance(values, pydt.time):
return dates.date2num(values)
elif is_integer(values) or is_float(values):
Expand Down Expand Up @@ -553,7 +553,7 @@ def _daily_finder(vmin, vmax, freq: BaseOffset):
elif dtype_code == FreqGroup.FR_DAY:
periodsperyear = 365
periodspermonth = 28
elif get_freq_group(dtype_code) == FreqGroup.FR_WK:
elif FreqGroup.get_freq_group(dtype_code) == FreqGroup.FR_WK:
periodsperyear = 52
periodspermonth = 3
else: # pragma: no cover
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/tseries/frequencies/test_freq_code.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest

from pandas._libs.tslibs import Resolution, to_offset
from pandas._libs.tslibs.frequencies import _attrname_to_abbrevs, get_to_timestamp_base
from pandas._libs.tslibs.dtypes import _attrname_to_abbrevs
from pandas._libs.tslibs.frequencies import get_to_timestamp_base


@pytest.mark.parametrize(
Expand Down