Skip to content

REF: move get_rule_month to libparsing to simplify dep structure #34284

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 1 commit into from
May 25, 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
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/frequencies.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
cdef dict attrname_to_abbrevs

cpdef str get_rule_month(object source, str default=*)

cpdef get_freq_code(freqstr)
cpdef int get_to_timestamp_base(int base)
cpdef str get_freq_str(base, mult=*)
33 changes: 1 addition & 32 deletions pandas/_libs/tslibs/frequencies.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from pandas._libs.tslibs.util cimport is_integer_object

from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
from pandas._libs.tslibs.offsets cimport is_offset_object
from pandas._libs.tslibs.parsing cimport get_rule_month

# ----------------------------------------------------------------------
# Constants
Expand Down Expand Up @@ -490,35 +491,3 @@ cdef bint _is_monthly(str rule):
cdef bint _is_weekly(str rule):
rule = rule.upper()
return rule == 'W' or rule.startswith('W-')


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

cpdef str get_rule_month(object source, str default="DEC"):
"""
Return starting month of given freq, default is December.

Parameters
----------
source : object
default : str, default "DEC"

Returns
-------
rule_month: str

Examples
--------
>>> get_rule_month('D')
'DEC'

>>> get_rule_month('A-JAN')
'JAN'
"""
if hasattr(source, 'freqstr'):
source = source.freqstr
source = source.upper()
if '-' not in source:
return default
else:
return source.split('-')[1]
2 changes: 2 additions & 0 deletions pandas/_libs/tslibs/parsing.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

cpdef str get_rule_month(object source, str default=*)
32 changes: 31 additions & 1 deletion pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ from pandas._libs.tslibs.util cimport (
is_array,
get_c_string_buf_and_size,
)
from pandas._libs.tslibs.frequencies cimport get_rule_month
from pandas._libs.tslibs.offsets cimport is_offset_object

cdef extern from "../src/headers/portable.h":
Expand Down Expand Up @@ -1019,3 +1018,34 @@ def concat_date_cols(tuple date_cols, bint keep_trivial_numbers=True):
result_view[row_idx] = " ".join(list_to_join)

return result


# TODO: `default` never used?
cpdef str get_rule_month(object source, str default="DEC"):
"""
Return starting month of given freq, default is December.

Parameters
----------
source : object
default : str, default "DEC"

Returns
-------
rule_month: str

Examples
--------
>>> get_rule_month('D')
'DEC'

>>> get_rule_month('A-JAN')
'JAN'
"""
if is_offset_object(source):
source = source.freqstr
source = source.upper()
if "-" not in source:
return default
else:
return source.split("-")[1]
3 changes: 2 additions & 1 deletion pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ from pandas._libs.tslibs.frequencies cimport (
attrname_to_abbrevs,
get_freq_code,
get_freq_str,
get_rule_month,
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 (
_nat_scalar_rules,
Expand Down Expand Up @@ -2506,6 +2506,7 @@ def quarter_to_myear(year: int, quarter: int, freq):
year -= 1

return year, month
# TODO: This whole func is really similar to parsing.pyx L434-L450


def validate_end_alias(how):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/tslibs/test_libfrequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from pandas._libs.tslibs.frequencies import (
INVALID_FREQ_ERR_MSG,
_period_str_to_code,
get_rule_month,
is_subperiod,
is_superperiod,
)
from pandas._libs.tslibs.parsing import get_rule_month

from pandas.tseries import offsets

Expand Down