Skip to content

REF: remove redundant get_freq function #34196

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
May 17, 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: 0 additions & 1 deletion pandas/_libs/tslibs/frequencies.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cpdef str get_rule_month(object source, str default=*)

cpdef get_freq_code(freqstr)
cpdef object get_freq(object freq)
cpdef str get_base_alias(freqstr)
cpdef int get_to_timestamp_base(int base)
cpdef str get_freq_str(base, mult=*)
19 changes: 0 additions & 19 deletions pandas/_libs/tslibs/frequencies.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -306,25 +306,6 @@ cpdef int get_to_timestamp_base(int base):
return base


cpdef object get_freq(object freq):
"""
Return frequency code of given frequency str.
If input is not string, return input as it is.

Examples
--------
>>> get_freq('A')
1000

>>> get_freq('3A')
1000
"""
if isinstance(freq, str):
base, mult = get_freq_code(freq)
freq = base
return freq


# ----------------------------------------------------------------------
# Frequency comparison

Expand Down
8 changes: 4 additions & 4 deletions pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from pandas._libs import lib, tslibs
from pandas._libs.tslibs import resolution
from pandas._libs.tslibs.frequencies import FreqGroup, get_freq
from pandas._libs.tslibs.frequencies import FreqGroup, get_freq_code

from pandas.core.dtypes.common import (
is_datetime64_ns_dtype,
Expand Down Expand Up @@ -887,7 +887,7 @@ def _annual_finder(vmin, vmax, freq):

def get_finder(freq):
if isinstance(freq, str):
freq = get_freq(freq)
freq = get_freq_code(freq)[0]
fgroup = resolution.get_freq_group(freq)

if fgroup == FreqGroup.FR_ANN:
Expand Down Expand Up @@ -932,7 +932,7 @@ def __init__(
plot_obj=None,
):
if isinstance(freq, str):
freq = get_freq(freq)
freq = get_freq_code(freq)[0]
self.freq = freq
self.base = base
(self.quarter, self.month, self.day) = (quarter, month, day)
Expand Down Expand Up @@ -1011,7 +1011,7 @@ class TimeSeries_DateFormatter(Formatter):

def __init__(self, freq, minor_locator=False, dynamic_mode=True, plot_obj=None):
if isinstance(freq, str):
freq = get_freq(freq)
freq = get_freq_code(freq)[0]
self.format = None
self.freq = freq
self.locs = []
Expand Down
6 changes: 3 additions & 3 deletions pandas/plotting/_matplotlib/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pandas._libs.tslibs.frequencies import (
FreqGroup,
get_base_alias,
get_freq,
get_freq_code,
is_subperiod,
is_superperiod,
)
Expand Down Expand Up @@ -209,9 +209,9 @@ def _use_dynamic_x(ax, data):
if freq is None:
return False

# hack this for 0.10.1, creating more technical debt...sigh
# FIXME: hack this for 0.10.1, creating more technical debt...sigh
if isinstance(data.index, ABCDatetimeIndex):
base = get_freq(freq)
base = get_freq_code(freq)[0]
x = data.index
if base <= FreqGroup.FR_DAY:
return x[:1].is_normalized
Expand Down
47 changes: 21 additions & 26 deletions pandas/tests/tseries/frequencies/test_freq_code.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import pytest

from pandas._libs.tslibs import frequencies as libfrequencies, resolution
from pandas._libs.tslibs.frequencies import (
FreqGroup,
_period_code_map,
get_freq,
get_freq_code,
)
from pandas._libs.tslibs.frequencies import FreqGroup, _period_code_map, get_freq_code

import pandas.tseries.offsets as offsets

Expand All @@ -31,12 +26,12 @@ def period_code_item(request):
],
)
def test_freq_code(freqstr, expected):
assert get_freq(freqstr) == expected
assert get_freq_code(freqstr)[0] == expected


def test_freq_code_match(period_code_item):
freqstr, code = period_code_item
assert get_freq(freqstr) == code
assert get_freq_code(freqstr)[0] == code


@pytest.mark.parametrize(
Expand Down Expand Up @@ -156,31 +151,31 @@ def test_cat(args):
"freq_input,expected",
[
# Frequency string.
("A", (get_freq("A"), 1)),
("3D", (get_freq("D"), 3)),
("-2M", (get_freq("M"), -2)),
("A", (get_freq_code("A")[0], 1)),
("3D", (get_freq_code("D")[0], 3)),
("-2M", (get_freq_code("M")[0], -2)),
# Tuple.
(("D", 1), (get_freq("D"), 1)),
(("A", 3), (get_freq("A"), 3)),
(("M", -2), (get_freq("M"), -2)),
(("D", 1), (get_freq_code("D")[0], 1)),
(("A", 3), (get_freq_code("A")[0], 3)),
(("M", -2), (get_freq_code("M")[0], -2)),
((5, "T"), (FreqGroup.FR_MIN, 5)),
# Numeric Tuple.
((1000, 1), (1000, 1)),
# Offsets.
(offsets.Day(), (get_freq("D"), 1)),
(offsets.Day(3), (get_freq("D"), 3)),
(offsets.Day(-2), (get_freq("D"), -2)),
(offsets.MonthEnd(), (get_freq("M"), 1)),
(offsets.MonthEnd(3), (get_freq("M"), 3)),
(offsets.MonthEnd(-2), (get_freq("M"), -2)),
(offsets.Week(), (get_freq("W"), 1)),
(offsets.Week(3), (get_freq("W"), 3)),
(offsets.Week(-2), (get_freq("W"), -2)),
(offsets.Day(), (get_freq_code("D")[0], 1)),
(offsets.Day(3), (get_freq_code("D")[0], 3)),
(offsets.Day(-2), (get_freq_code("D")[0], -2)),
(offsets.MonthEnd(), (get_freq_code("M")[0], 1)),
(offsets.MonthEnd(3), (get_freq_code("M")[0], 3)),
(offsets.MonthEnd(-2), (get_freq_code("M")[0], -2)),
(offsets.Week(), (get_freq_code("W")[0], 1)),
(offsets.Week(3), (get_freq_code("W")[0], 3)),
(offsets.Week(-2), (get_freq_code("W")[0], -2)),
(offsets.Hour(), (FreqGroup.FR_HR, 1)),
# Monday is weekday=0.
(offsets.Week(weekday=1), (get_freq("W-TUE"), 1)),
(offsets.Week(3, weekday=0), (get_freq("W-MON"), 3)),
(offsets.Week(-2, weekday=4), (get_freq("W-FRI"), -2)),
(offsets.Week(weekday=1), (get_freq_code("W-TUE")[0], 1)),
(offsets.Week(3, weekday=0), (get_freq_code("W-MON")[0], 3)),
(offsets.Week(-2, weekday=4), (get_freq_code("W-FRI")[0], -2)),
],
)
def test_get_freq_code(freq_input, expected):
Expand Down
13 changes: 8 additions & 5 deletions pandas/tests/tslibs/test_period_asfreq.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from pandas._libs.tslibs.frequencies import get_freq
from pandas._libs.tslibs.frequencies import get_freq_code
from pandas._libs.tslibs.period import period_asfreq, period_ordinal


Expand Down Expand Up @@ -31,15 +31,18 @@
],
)
def test_intra_day_conversion_factors(freq1, freq2, expected):
assert period_asfreq(1, get_freq(freq1), get_freq(freq2), False) == expected
assert (
period_asfreq(1, get_freq_code(freq1)[0], get_freq_code(freq2)[0], False)
== expected
)


@pytest.mark.parametrize(
"freq,expected", [("A", 0), ("M", 0), ("W", 1), ("D", 0), ("B", 0)]
)
def test_period_ordinal_start_values(freq, expected):
# information for Jan. 1, 1970.
assert period_ordinal(1970, 1, 1, 0, 0, 0, 0, 0, get_freq(freq)) == expected
assert period_ordinal(1970, 1, 1, 0, 0, 0, 0, 0, get_freq_code(freq)[0]) == expected


@pytest.mark.parametrize(
Expand All @@ -52,7 +55,7 @@ def test_period_ordinal_start_values(freq, expected):
],
)
def test_period_ordinal_week(dt, expected):
args = dt + (get_freq("W"),)
args = dt + (get_freq_code("W")[0],)
assert period_ordinal(*args) == expected


Expand All @@ -74,5 +77,5 @@ def test_period_ordinal_week(dt, expected):
],
)
def test_period_ordinal_business_day(day, expected):
args = (2013, 10, day, 0, 0, 0, 0, 0, get_freq("B"))
args = (2013, 10, day, 0, 0, 0, 0, 0, get_freq_code("B")[0])
assert period_ordinal(*args) == expected