Skip to content

Commit 7f50f71

Browse files
authored
CLN: tighten types to get_rule_month (#35205)
1 parent df24c10 commit 7f50f71

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

pandas/_libs/tslibs/parsing.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
cpdef str get_rule_month(object source, str default=*)
2+
cpdef str get_rule_month(str source)

pandas/_libs/tslibs/parsing.pyx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def parse_time_string(arg: str, freq=None, dayfirst=None, yearfirst=None):
284284

285285

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

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

10221023

1023-
# TODO: `default` never used?
1024-
cpdef str get_rule_month(object source, str default="DEC"):
1024+
cpdef str get_rule_month(str source):
10251025
"""
10261026
Return starting month of given freq, default is December.
10271027
10281028
Parameters
10291029
----------
1030-
source : object
1031-
default : str, default "DEC"
1030+
source : str
1031+
Derived from `freq.rule_code` or `freq.freqstr`.
10321032
10331033
Returns
10341034
-------
@@ -1042,10 +1042,8 @@ cpdef str get_rule_month(object source, str default="DEC"):
10421042
>>> get_rule_month('A-JAN')
10431043
'JAN'
10441044
"""
1045-
if is_offset_object(source):
1046-
source = source.freqstr
10471045
source = source.upper()
10481046
if "-" not in source:
1049-
return default
1047+
return "DEC"
10501048
else:
10511049
return source.split("-")[1]

pandas/_libs/tslibs/period.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,13 +2440,13 @@ cdef int64_t _ordinal_from_fields(int year, int month, quarter, int day,
24402440
BaseOffset freq):
24412441
base = freq_to_dtype_code(freq)
24422442
if quarter is not None:
2443-
year, month = quarter_to_myear(year, quarter, freq)
2443+
year, month = quarter_to_myear(year, quarter, freq.freqstr)
24442444

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

24482448

2449-
def quarter_to_myear(year: int, quarter: int, freq):
2449+
def quarter_to_myear(year: int, quarter: int, freqstr: str):
24502450
"""
24512451
A quarterly frequency defines a "year" which may not coincide with
24522452
the calendar-year. Find the calendar-year and calendar-month associated
@@ -2456,7 +2456,8 @@ def quarter_to_myear(year: int, quarter: int, freq):
24562456
----------
24572457
year : int
24582458
quarter : int
2459-
freq : DateOffset
2459+
freqstr : str
2460+
Equivalent to freq.freqstr
24602461
24612462
Returns
24622463
-------
@@ -2470,7 +2471,7 @@ def quarter_to_myear(year: int, quarter: int, freq):
24702471
if quarter <= 0 or quarter > 4:
24712472
raise ValueError('Quarter must be 1 <= q <= 4')
24722473

2473-
mnum = c_MONTH_NUMBERS[get_rule_month(freq)] + 1
2474+
mnum = c_MONTH_NUMBERS[get_rule_month(freqstr)] + 1
24742475
month = (mnum + (quarter - 1) * 3) % 12 + 1
24752476
if month > mnum:
24762477
year -= 1

pandas/core/arrays/period.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,9 +1034,10 @@ def _range_from_fields(
10341034
if base != FreqGroup.FR_QTR:
10351035
raise AssertionError("base must equal FR_QTR")
10361036

1037+
freqstr = freq.freqstr
10371038
year, quarter = _make_field_arrays(year, quarter)
10381039
for y, q in zip(year, quarter):
1039-
y, m = libperiod.quarter_to_myear(y, q, freq)
1040+
y, m = libperiod.quarter_to_myear(y, q, freqstr)
10401041
val = libperiod.period_ordinal(y, m, 1, 1, 1, 1, 0, 0, base)
10411042
ordinals.append(val)
10421043
else:

pandas/tests/tslibs/test_libfrequencies.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
"obj,expected",
1010
[
1111
("W", "DEC"),
12-
(offsets.Week(), "DEC"),
12+
(offsets.Week().freqstr, "DEC"),
1313
("D", "DEC"),
14-
(offsets.Day(), "DEC"),
14+
(offsets.Day().freqstr, "DEC"),
1515
("Q", "DEC"),
16-
(offsets.QuarterEnd(startingMonth=12), "DEC"),
16+
(offsets.QuarterEnd(startingMonth=12).freqstr, "DEC"),
1717
("Q-JAN", "JAN"),
18-
(offsets.QuarterEnd(startingMonth=1), "JAN"),
18+
(offsets.QuarterEnd(startingMonth=1).freqstr, "JAN"),
1919
("A-DEC", "DEC"),
2020
("Y-DEC", "DEC"),
21-
(offsets.YearEnd(), "DEC"),
21+
(offsets.YearEnd().freqstr, "DEC"),
2222
("A-MAY", "MAY"),
2323
("Y-MAY", "MAY"),
24-
(offsets.YearEnd(month=5), "MAY"),
24+
(offsets.YearEnd(month=5).freqstr, "MAY"),
2525
],
2626
)
2727
def test_get_rule_month(obj, expected):

0 commit comments

Comments
 (0)