From 54b1bf957f0d90079c23ed16147e99a73b9a31f0 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 20 May 2020 18:33:11 -0700 Subject: [PATCH] REF: move get_rule_month to libparsing to simplify dep structure --- pandas/_libs/tslibs/frequencies.pxd | 2 -- pandas/_libs/tslibs/frequencies.pyx | 33 +--------------------- pandas/_libs/tslibs/parsing.pxd | 2 ++ pandas/_libs/tslibs/parsing.pyx | 32 ++++++++++++++++++++- pandas/_libs/tslibs/period.pyx | 3 +- pandas/tests/tslibs/test_libfrequencies.py | 2 +- 6 files changed, 37 insertions(+), 37 deletions(-) create mode 100644 pandas/_libs/tslibs/parsing.pxd diff --git a/pandas/_libs/tslibs/frequencies.pxd b/pandas/_libs/tslibs/frequencies.pxd index 09397a02c5ea0..098944c965df0 100644 --- a/pandas/_libs/tslibs/frequencies.pxd +++ b/pandas/_libs/tslibs/frequencies.pxd @@ -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=*) diff --git a/pandas/_libs/tslibs/frequencies.pyx b/pandas/_libs/tslibs/frequencies.pyx index 23a02da62a47b..0fec4bca96251 100644 --- a/pandas/_libs/tslibs/frequencies.pyx +++ b/pandas/_libs/tslibs/frequencies.pyx @@ -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 @@ -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] diff --git a/pandas/_libs/tslibs/parsing.pxd b/pandas/_libs/tslibs/parsing.pxd new file mode 100644 index 0000000000000..6e826cd4c6602 --- /dev/null +++ b/pandas/_libs/tslibs/parsing.pxd @@ -0,0 +1,2 @@ + +cpdef str get_rule_month(object source, str default=*) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index d047bf49358a6..bf895f155fc59 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -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": @@ -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] diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 9bcf162b6d666..350ee3f552094 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -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, @@ -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): diff --git a/pandas/tests/tslibs/test_libfrequencies.py b/pandas/tests/tslibs/test_libfrequencies.py index 5810c7e52abca..2dba0f51ca217 100644 --- a/pandas/tests/tslibs/test_libfrequencies.py +++ b/pandas/tests/tslibs/test_libfrequencies.py @@ -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