Skip to content

REF: remove roll_check, use roll_convention #34763

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 2 commits into from
Jun 14, 2020
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
21 changes: 6 additions & 15 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3736,7 +3736,6 @@ cdef shift_quarters(
npy_datetimestruct dts
int count = len(dtindex)
int months_to_roll, months_since, n, compare_day
bint roll_check
int64_t[:] out = np.empty(count, dtype="int64")

if day_opt == "start":
Expand Down Expand Up @@ -3878,7 +3877,6 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):
npy_datetimestruct dts
int count = len(dtindex)
int months_to_roll
bint roll_check
int64_t[:] out = np.empty(count, dtype="int64")

if day_opt is None:
Expand All @@ -3895,10 +3893,6 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):
dts.day = min(dts.day, get_days_in_month(dts.year, dts.month))
out[i] = dtstruct_to_dt64(&dts)
elif day_opt == "start":
roll_check = False
if months <= 0:
months += 1
roll_check = True
with nogil:
for i in range(count):
if dtindex[i] == NPY_NAT:
Expand All @@ -3907,22 +3901,19 @@ 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

# offset semantics - if on the anchor point and going backwards
# shift to next
if roll_check and dts.day == 1:
months_to_roll -= 1
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 = 1

out[i] = dtstruct_to_dt64(&dts)
elif day_opt == "end":
roll_check = False
if months > 0:
months -= 1
roll_check = True
with nogil:
for i in range(count):
if dtindex[i] == NPY_NAT:
Expand All @@ -3931,12 +3922,12 @@ 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)

# similar semantics - when adding shift forward by one
# month if already at an end of month
if roll_check and dts.day == get_days_in_month(dts.year,
dts.month):
months_to_roll += 1
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)
Expand Down