Skip to content

CLN: tighten types to get_rule_month #35205

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
Jul 10, 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: 1 addition & 1 deletion pandas/_libs/tslibs/parsing.pxd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

cpdef str get_rule_month(object source, str default=*)
cpdef str get_rule_month(str source)
14 changes: 6 additions & 8 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def parse_time_string(arg: str, freq=None, dayfirst=None, yearfirst=None):


cdef parse_datetime_string_with_reso(
str date_string, object freq=None, bint dayfirst=False, bint yearfirst=False,
str date_string, str freq=None, bint dayfirst=False, bint yearfirst=False,
):
"""
Parse datetime string and try to identify its resolution.
Expand Down Expand Up @@ -438,6 +438,7 @@ cdef inline object _parse_dateabbr_string(object date_string, datetime default,

if freq is not None:
# TODO: hack attack, #1228
freq = getattr(freq, "freqstr", freq)
try:
mnum = c_MONTH_NUMBERS[get_rule_month(freq)] + 1
except (KeyError, ValueError):
Expand Down Expand Up @@ -1020,15 +1021,14 @@ def concat_date_cols(tuple date_cols, bint keep_trivial_numbers=True):
return result


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

Parameters
----------
source : object
default : str, default "DEC"
source : str
Derived from `freq.rule_code` or `freq.freqstr`.

Returns
-------
Expand All @@ -1042,10 +1042,8 @@ cpdef str get_rule_month(object source, str default="DEC"):
>>> get_rule_month('A-JAN')
'JAN'
"""
if is_offset_object(source):
source = source.freqstr
source = source.upper()
if "-" not in source:
return default
return "DEC"
else:
return source.split("-")[1]
9 changes: 5 additions & 4 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2440,13 +2440,13 @@ cdef int64_t _ordinal_from_fields(int year, int month, quarter, int day,
BaseOffset freq):
base = freq_to_dtype_code(freq)
if quarter is not None:
year, month = quarter_to_myear(year, quarter, freq)
year, month = quarter_to_myear(year, quarter, freq.freqstr)

return period_ordinal(year, month, day, hour,
minute, second, 0, 0, base)


def quarter_to_myear(year: int, quarter: int, freq):
def quarter_to_myear(year: int, quarter: int, freqstr: str):
"""
A quarterly frequency defines a "year" which may not coincide with
the calendar-year. Find the calendar-year and calendar-month associated
Expand All @@ -2456,7 +2456,8 @@ def quarter_to_myear(year: int, quarter: int, freq):
----------
year : int
quarter : int
freq : DateOffset
freqstr : str
Equivalent to freq.freqstr

Returns
-------
Expand All @@ -2470,7 +2471,7 @@ def quarter_to_myear(year: int, quarter: int, freq):
if quarter <= 0 or quarter > 4:
raise ValueError('Quarter must be 1 <= q <= 4')

mnum = c_MONTH_NUMBERS[get_rule_month(freq)] + 1
mnum = c_MONTH_NUMBERS[get_rule_month(freqstr)] + 1
month = (mnum + (quarter - 1) * 3) % 12 + 1
if month > mnum:
year -= 1
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,10 @@ def _range_from_fields(
if base != FreqGroup.FR_QTR:
raise AssertionError("base must equal FR_QTR")

freqstr = freq.freqstr
year, quarter = _make_field_arrays(year, quarter)
for y, q in zip(year, quarter):
y, m = libperiod.quarter_to_myear(y, q, freq)
y, m = libperiod.quarter_to_myear(y, q, freqstr)
val = libperiod.period_ordinal(y, m, 1, 1, 1, 1, 0, 0, base)
ordinals.append(val)
else:
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/tslibs/test_libfrequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
"obj,expected",
[
("W", "DEC"),
(offsets.Week(), "DEC"),
(offsets.Week().freqstr, "DEC"),
("D", "DEC"),
(offsets.Day(), "DEC"),
(offsets.Day().freqstr, "DEC"),
("Q", "DEC"),
(offsets.QuarterEnd(startingMonth=12), "DEC"),
(offsets.QuarterEnd(startingMonth=12).freqstr, "DEC"),
("Q-JAN", "JAN"),
(offsets.QuarterEnd(startingMonth=1), "JAN"),
(offsets.QuarterEnd(startingMonth=1).freqstr, "JAN"),
("A-DEC", "DEC"),
("Y-DEC", "DEC"),
(offsets.YearEnd(), "DEC"),
(offsets.YearEnd().freqstr, "DEC"),
("A-MAY", "MAY"),
("Y-MAY", "MAY"),
(offsets.YearEnd(month=5), "MAY"),
(offsets.YearEnd(month=5).freqstr, "MAY"),
],
)
def test_get_rule_month(obj, expected):
Expand Down