Skip to content

Commit af66d64

Browse files
authored
REF: move get_rule_month to libparsing to simplify dep structure (#34284)
1 parent a087f3e commit af66d64

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

pandas/_libs/tslibs/frequencies.pxd

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
cdef dict attrname_to_abbrevs
22

3-
cpdef str get_rule_month(object source, str default=*)
4-
53
cpdef get_freq_code(freqstr)
64
cpdef int get_to_timestamp_base(int base)
75
cpdef str get_freq_str(base, mult=*)

pandas/_libs/tslibs/frequencies.pyx

+1-32
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ from pandas._libs.tslibs.util cimport is_integer_object
77

88
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
99
from pandas._libs.tslibs.offsets cimport is_offset_object
10+
from pandas._libs.tslibs.parsing cimport get_rule_month
1011

1112
# ----------------------------------------------------------------------
1213
# Constants
@@ -490,35 +491,3 @@ cdef bint _is_monthly(str rule):
490491
cdef bint _is_weekly(str rule):
491492
rule = rule.upper()
492493
return rule == 'W' or rule.startswith('W-')
493-
494-
495-
# ----------------------------------------------------------------------
496-
497-
cpdef str get_rule_month(object source, str default="DEC"):
498-
"""
499-
Return starting month of given freq, default is December.
500-
501-
Parameters
502-
----------
503-
source : object
504-
default : str, default "DEC"
505-
506-
Returns
507-
-------
508-
rule_month: str
509-
510-
Examples
511-
--------
512-
>>> get_rule_month('D')
513-
'DEC'
514-
515-
>>> get_rule_month('A-JAN')
516-
'JAN'
517-
"""
518-
if hasattr(source, 'freqstr'):
519-
source = source.freqstr
520-
source = source.upper()
521-
if '-' not in source:
522-
return default
523-
else:
524-
return source.split('-')[1]

pandas/_libs/tslibs/parsing.pxd

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
cpdef str get_rule_month(object source, str default=*)

pandas/_libs/tslibs/parsing.pyx

+31-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ from pandas._libs.tslibs.util cimport (
4141
is_array,
4242
get_c_string_buf_and_size,
4343
)
44-
from pandas._libs.tslibs.frequencies cimport get_rule_month
4544
from pandas._libs.tslibs.offsets cimport is_offset_object
4645

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

10211020
return result
1021+
1022+
1023+
# TODO: `default` never used?
1024+
cpdef str get_rule_month(object source, str default="DEC"):
1025+
"""
1026+
Return starting month of given freq, default is December.
1027+
1028+
Parameters
1029+
----------
1030+
source : object
1031+
default : str, default "DEC"
1032+
1033+
Returns
1034+
-------
1035+
rule_month: str
1036+
1037+
Examples
1038+
--------
1039+
>>> get_rule_month('D')
1040+
'DEC'
1041+
1042+
>>> get_rule_month('A-JAN')
1043+
'JAN'
1044+
"""
1045+
if is_offset_object(source):
1046+
source = source.freqstr
1047+
source = source.upper()
1048+
if "-" not in source:
1049+
return default
1050+
else:
1051+
return source.split("-")[1]

pandas/_libs/tslibs/period.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ from pandas._libs.tslibs.frequencies cimport (
5454
attrname_to_abbrevs,
5555
get_freq_code,
5656
get_freq_str,
57-
get_rule_month,
5857
get_to_timestamp_base,
5958
)
59+
from pandas._libs.tslibs.parsing cimport get_rule_month
6060
from pandas._libs.tslibs.parsing import parse_time_string
6161
from pandas._libs.tslibs.nattype cimport (
6262
_nat_scalar_rules,
@@ -2511,6 +2511,7 @@ def quarter_to_myear(year: int, quarter: int, freq):
25112511
year -= 1
25122512

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

25152516

25162517
def validate_end_alias(how):

pandas/tests/tslibs/test_libfrequencies.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from pandas._libs.tslibs.frequencies import (
44
INVALID_FREQ_ERR_MSG,
55
_period_str_to_code,
6-
get_rule_month,
76
is_subperiod,
87
is_superperiod,
98
)
9+
from pandas._libs.tslibs.parsing import get_rule_month
1010

1111
from pandas.tseries import offsets
1212

0 commit comments

Comments
 (0)