Skip to content

Commit bae7952

Browse files
authored
REF: remove roll_check, use roll_convention (#34763)
1 parent f43a843 commit bae7952

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

pandas/_libs/tslibs/offsets.pyx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,7 +3744,6 @@ cdef shift_quarters(
37443744
npy_datetimestruct dts
37453745
int count = len(dtindex)
37463746
int months_to_roll, months_since, n, compare_day
3747-
bint roll_check
37483747
int64_t[:] out = np.empty(count, dtype="int64")
37493748

37503749
if day_opt == "start":
@@ -3886,7 +3885,6 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):
38863885
npy_datetimestruct dts
38873886
int count = len(dtindex)
38883887
int months_to_roll
3889-
bint roll_check
38903888
int64_t[:] out = np.empty(count, dtype="int64")
38913889

38923890
if day_opt is None:
@@ -3903,10 +3901,6 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):
39033901
dts.day = min(dts.day, get_days_in_month(dts.year, dts.month))
39043902
out[i] = dtstruct_to_dt64(&dts)
39053903
elif day_opt == "start":
3906-
roll_check = False
3907-
if months <= 0:
3908-
months += 1
3909-
roll_check = True
39103904
with nogil:
39113905
for i in range(count):
39123906
if dtindex[i] == NPY_NAT:
@@ -3915,22 +3909,19 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):
39153909

39163910
dt64_to_dtstruct(dtindex[i], &dts)
39173911
months_to_roll = months
3912+
compare_day = 1
39183913

39193914
# offset semantics - if on the anchor point and going backwards
39203915
# shift to next
3921-
if roll_check and dts.day == 1:
3922-
months_to_roll -= 1
3916+
months_to_roll = roll_convention(dts.day, months_to_roll,
3917+
compare_day)
39233918

39243919
dts.year = year_add_months(dts, months_to_roll)
39253920
dts.month = month_add_months(dts, months_to_roll)
39263921
dts.day = 1
39273922

39283923
out[i] = dtstruct_to_dt64(&dts)
39293924
elif day_opt == "end":
3930-
roll_check = False
3931-
if months > 0:
3932-
months -= 1
3933-
roll_check = True
39343925
with nogil:
39353926
for i in range(count):
39363927
if dtindex[i] == NPY_NAT:
@@ -3939,12 +3930,12 @@ def shift_months(const int64_t[:] dtindex, int months, object day_opt=None):
39393930

39403931
dt64_to_dtstruct(dtindex[i], &dts)
39413932
months_to_roll = months
3933+
compare_day = get_days_in_month(dts.year, dts.month)
39423934

39433935
# similar semantics - when adding shift forward by one
39443936
# month if already at an end of month
3945-
if roll_check and dts.day == get_days_in_month(dts.year,
3946-
dts.month):
3947-
months_to_roll += 1
3937+
months_to_roll = roll_convention(dts.day, months_to_roll,
3938+
compare_day)
39483939

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

0 commit comments

Comments
 (0)