Skip to content

Commit 23e208f

Browse files
authored
REF: implement _roll_qtrday (#34781)
1 parent 200abb2 commit 23e208f

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

pandas/_libs/tslibs/offsets.pyx

+14-20
Original file line numberDiff line numberDiff line change
@@ -3786,7 +3786,7 @@ cdef inline void _shift_quarters(const int64_t[:] dtindex,
37863786
"""See shift_quarters.__doc__"""
37873787
cdef:
37883788
Py_ssize_t i
3789-
int months_since, compare_day, n
3789+
int months_since, n
37903790
npy_datetimestruct dts
37913791

37923792
for i in range(count):
@@ -3798,18 +3798,7 @@ cdef inline void _shift_quarters(const int64_t[:] dtindex,
37983798
n = quarters
37993799

38003800
months_since = (dts.month - q1start_month) % modby
3801-
compare_day = get_day_of_month(&dts, day_opt)
3802-
3803-
# offset semantics - if on the anchor point and going backwards
3804-
# shift to next
3805-
if n <= 0 and (months_since != 0 or
3806-
(months_since == 0 and dts.day > compare_day)):
3807-
# make sure to roll forward, so negate
3808-
n += 1
3809-
elif n > 0 and (months_since == 0 and dts.day < compare_day):
3810-
# pretend to roll back if on same month but
3811-
# before compare_day
3812-
n -= 1
3801+
n = _roll_qtrday(&dts, n, months_since, day_opt)
38133802

38143803
dts.year = year_add_months(dts, modby * n - months_since)
38153804
dts.month = month_add_months(dts, modby * n - months_since)
@@ -4009,7 +3998,7 @@ cpdef int roll_convention(int other, int n, int compare) nogil:
40093998

40103999

40114000
def roll_qtrday(other: datetime, n: int, month: int,
4012-
day_opt: object, modby: int=3) -> int:
4001+
day_opt: object, modby: int) -> int:
40134002
"""
40144003
Possibly increment or decrement the number of periods to shift
40154004
based on rollforward/rollbackward conventions.
@@ -4037,25 +4026,30 @@ def roll_qtrday(other: datetime, n: int, month: int,
40374026
npy_datetimestruct dts
40384027
pydate_to_dtstruct(other, &dts)
40394028

4040-
# TODO: with small adjustments this could be used in shift_quarters
4041-
40424029
if modby == 12:
40434030
# We care about the month-of-year, not month-of-quarter, so skip mod
40444031
months_since = other.month - month
40454032
else:
40464033
months_since = other.month % modby - month % modby
40474034

4035+
return _roll_qtrday(&dts, n, months_since, day_opt)
4036+
4037+
4038+
cdef inline int _roll_qtrday(npy_datetimestruct* dts,
4039+
int n,
4040+
int months_since,
4041+
str day_opt) nogil except? -1:
4042+
"""See roll_qtrday.__doc__"""
4043+
40484044
if n > 0:
40494045
if months_since < 0 or (months_since == 0 and
4050-
other.day < get_day_of_month(&dts,
4051-
day_opt)):
4046+
dts.day < get_day_of_month(dts, day_opt)):
40524047
# pretend to roll back if on same month but
40534048
# before compare_day
40544049
n -= 1
40554050
else:
40564051
if months_since > 0 or (months_since == 0 and
4057-
other.day > get_day_of_month(&dts,
4058-
day_opt)):
4052+
dts.day > get_day_of_month(dts, day_opt)):
40594053
# make sure to roll forward, so negate
40604054
n += 1
40614055
return n

0 commit comments

Comments
 (0)