Skip to content

REF: inline get_day_of_month #34772

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
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
53 changes: 29 additions & 24 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3757,16 +3757,22 @@ cdef shift_quarters(
n = quarters

months_since = (dts.month - q1start_month) % modby
compare_day = get_day_of_month(&dts, day_opt)

# offset semantics - if on the anchor point and going backwards
# shift to next
if n <= 0 and (months_since != 0 or
(months_since == 0 and dts.day > 1)):
(months_since == 0 and dts.day > compare_day)):
# make sure to roll forward, so negate
n += 1
elif n > 0 and (months_since == 0 and dts.day < compare_day):
# pretend to roll back if on same month but
# before compare_day
n -= 1

dts.year = year_add_months(dts, modby * n - months_since)
dts.month = month_add_months(dts, modby * n - months_since)
dts.day = 1
dts.day = get_day_of_month(&dts, day_opt)

out[i] = dtstruct_to_dt64(&dts)

Expand All @@ -3781,21 +3787,20 @@ cdef shift_quarters(
n = quarters

months_since = (dts.month - q1start_month) % modby
compare_day = get_day_of_month(&dts, day_opt)

if n <= 0 and months_since != 0:
# The general case of this condition would be
# `months_since != 0 or (months_since == 0 and
# dts.day > get_days_in_month(dts.year, dts.month))`
# but the get_days_in_month inequality would never hold.
if n <= 0 and (months_since != 0 or
(months_since == 0 and dts.day > compare_day)):
# make sure to roll forward, so negate
n += 1
elif n > 0 and (months_since == 0 and
dts.day < get_days_in_month(dts.year,
dts.month)):
elif n > 0 and (months_since == 0 and dts.day < compare_day):
# pretend to roll back if on same month but
# before compare_day
n -= 1

dts.year = year_add_months(dts, modby * n - months_since)
dts.month = month_add_months(dts, modby * n - months_since)
dts.day = get_days_in_month(dts.year, dts.month)
dts.day = get_day_of_month(&dts, day_opt)

out[i] = dtstruct_to_dt64(&dts)

Expand All @@ -3812,7 +3817,7 @@ cdef shift_quarters(
months_since = (dts.month - q1start_month) % modby
# compare_day is only relevant for comparison in the case
# where months_since == 0.
compare_day = get_firstbday(dts.year, dts.month)
compare_day = get_day_of_month(&dts, day_opt)

if n <= 0 and (months_since != 0 or
(months_since == 0 and dts.day > compare_day)):
Expand All @@ -3826,7 +3831,7 @@ cdef shift_quarters(
dts.year = year_add_months(dts, modby * n - months_since)
dts.month = month_add_months(dts, modby * n - months_since)

dts.day = get_firstbday(dts.year, dts.month)
dts.day = get_day_of_month(&dts, day_opt)

out[i] = dtstruct_to_dt64(&dts)

Expand All @@ -3843,7 +3848,7 @@ cdef shift_quarters(
months_since = (dts.month - q1start_month) % modby
# compare_day is only relevant for comparison in the case
# where months_since == 0.
compare_day = get_lastbday(dts.year, dts.month)
compare_day = get_day_of_month(&dts, day_opt)

if n <= 0 and (months_since != 0 or
(months_since == 0 and dts.day > compare_day)):
Expand All @@ -3857,7 +3862,7 @@ cdef shift_quarters(
dts.year = year_add_months(dts, modby * n - months_since)
dts.month = month_add_months(dts, modby * n - months_since)

dts.day = get_lastbday(dts.year, dts.month)
dts.day = get_day_of_month(&dts, day_opt)

out[i] = dtstruct_to_dt64(&dts)

Expand Down Expand Up @@ -3909,7 +3914,7 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):

dt64_to_dtstruct(dtindex[i], &dts)
months_to_roll = months
compare_day = 1
compare_day = get_day_of_month(&dts, day_opt)

# offset semantics - if on the anchor point and going backwards
# shift to next
Expand All @@ -3918,7 +3923,7 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):

dts.year = year_add_months(dts, months_to_roll)
dts.month = month_add_months(dts, months_to_roll)
dts.day = 1
dts.day = get_day_of_month(&dts, day_opt)

out[i] = dtstruct_to_dt64(&dts)
elif day_opt == "end":
Expand All @@ -3930,7 +3935,7 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):

dt64_to_dtstruct(dtindex[i], &dts)
months_to_roll = months
compare_day = get_days_in_month(dts.year, dts.month)
compare_day = get_day_of_month(&dts, day_opt)

# similar semantics - when adding shift forward by one
# month if already at an end of month
Expand All @@ -3940,7 +3945,7 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):
dts.year = year_add_months(dts, months_to_roll)
dts.month = month_add_months(dts, months_to_roll)

dts.day = get_days_in_month(dts.year, dts.month)
dts.day = get_day_of_month(&dts, day_opt)
out[i] = dtstruct_to_dt64(&dts)

elif day_opt == "business_start":
Expand All @@ -3952,15 +3957,15 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):

dt64_to_dtstruct(dtindex[i], &dts)
months_to_roll = months
compare_day = get_firstbday(dts.year, dts.month)
compare_day = get_day_of_month(&dts, day_opt)

months_to_roll = roll_convention(dts.day, months_to_roll,
compare_day)

dts.year = year_add_months(dts, months_to_roll)
dts.month = month_add_months(dts, months_to_roll)

dts.day = get_firstbday(dts.year, dts.month)
dts.day = get_day_of_month(&dts, day_opt)
out[i] = dtstruct_to_dt64(&dts)

elif day_opt == "business_end":
Expand All @@ -3972,15 +3977,15 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):

dt64_to_dtstruct(dtindex[i], &dts)
months_to_roll = months
compare_day = get_lastbday(dts.year, dts.month)
compare_day = get_day_of_month(&dts, day_opt)

months_to_roll = roll_convention(dts.day, months_to_roll,
compare_day)

dts.year = year_add_months(dts, months_to_roll)
dts.month = month_add_months(dts, months_to_roll)

dts.day = get_lastbday(dts.year, dts.month)
dts.day = get_day_of_month(&dts, day_opt)
out[i] = dtstruct_to_dt64(&dts)

else:
Expand Down Expand Up @@ -4051,7 +4056,7 @@ def shift_month(stamp: datetime, months: int,
return stamp.replace(year=year, month=month, day=day)


cdef int get_day_of_month(npy_datetimestruct* dts, day_opt) nogil except? -1:
cdef inline int get_day_of_month(npy_datetimestruct* dts, day_opt) nogil except? -1:
"""
Find the day in `other`'s month that satisfies a DateOffset's is_on_offset
policy, as described by the `day_opt` argument.
Expand Down