@@ -1879,7 +1879,7 @@ cdef class YearOffset(SingleConstructorOffset):
1879
1879
1880
1880
@apply_wraps
1881
1881
def apply(self , other ):
1882
- years = roll_yearday (other, self .n, self .month, self ._day_opt)
1882
+ years = roll_qtrday (other, self .n, self .month, self ._day_opt, modby = 12 )
1883
1883
months = years * 12 + (self .month - other.month)
1884
1884
return shift_month(other, months, self ._day_opt)
1885
1885
@@ -4158,10 +4158,13 @@ def roll_qtrday(other: datetime, n: int, month: int,
4158
4158
npy_datetimestruct dts
4159
4159
pydate_to_dtstruct(other , &dts )
4160
4160
4161
- # TODO: Merge this with roll_yearday by setting modby = 12 there?
4162
- # code de-duplication versus perf hit?
4163
4161
# TODO: with small adjustments this could be used in shift_quarters
4164
- months_since = other.month % modby - month % modby
4162
+
4163
+ if modby == 12:
4164
+ # We care about the month-of-year , not month-of-quarter , so skip mod
4165
+ months_since = other.month - month
4166
+ else:
4167
+ months_since = other.month % modby - month % modby
4165
4168
4166
4169
if n > 0:
4167
4170
if months_since < 0 or (months_since == 0 and
@@ -4177,84 +4180,3 @@ def roll_qtrday(other: datetime, n: int, month: int,
4177
4180
# make sure to roll forward, so negate
4178
4181
n += 1
4179
4182
return n
4180
-
4181
-
4182
- def roll_yearday (other: datetime , n: int , month: int , day_opt: object ) -> int:
4183
- """
4184
- Possibly increment or decrement the number of periods to shift
4185
- based on rollforward/rollbackward conventions.
4186
-
4187
- Parameters
4188
- ----------
4189
- other : datetime or Timestamp
4190
- n : number of periods to increment , before adjusting for rolling
4191
- month : reference month giving the first month of the year
4192
- day_opt : 'start', 'end', 'business_start', 'business_end', or int
4193
- The day of the month to compare against that of `other` when
4194
- incrementing or decrementing the number of periods:
4195
-
4196
- 'start': 1
4197
- 'end': last day of the month
4198
- 'business_start': first business day of the month
4199
- 'business_end': last business day of the month
4200
- int: day in the month indicated by `other`, or the last of day
4201
- the month if the value exceeds in that month's number of days.
4202
-
4203
- Returns
4204
- -------
4205
- n : int number of periods to increment
4206
-
4207
- Notes
4208
- -----
4209
- * Mirrors `roll_check` in shift_months
4210
-
4211
- Examples
4212
- -------
4213
- >>> month = 3
4214
- >>> day_opt = ' start' # `other` will be compared to March 1
4215
- >>> other = datetime(2017 , 2 , 10 ) # before March 1
4216
- >>> roll_yearday(other , 2, month , day_opt )
4217
- 1
4218
- >>> roll_yearday(other , -7, month , day_opt )
4219
- -7
4220
- >>>
4221
- >>> other = Timestamp(' 2014-03-15' , tz = ' US/Eastern' ) # after March 1
4222
- >>> roll_yearday(other , 2, month , day_opt )
4223
- 2
4224
- >>> roll_yearday(other , -7, month , day_opt )
4225
- -6
4226
-
4227
- >>> month = 6
4228
- >>> day_opt = ' end' # `other` will be compared to June 30
4229
- >>> other = datetime(1999 , 6 , 29 ) # before June 30
4230
- >>> roll_yearday(other , 5, month , day_opt )
4231
- 4
4232
- >>> roll_yearday(other , -7, month , day_opt )
4233
- -7
4234
- >>>
4235
- >>> other = Timestamp(2072 , 8 , 24 , 6 , 17 , 18 ) # after June 30
4236
- >>> roll_yearday(other , 5, month , day_opt )
4237
- 5
4238
- >>> roll_yearday(other , -7, month , day_opt )
4239
- -6
4240
-
4241
- """
4242
- cdef:
4243
- npy_datetimestruct dts
4244
- pydate_to_dtstruct(other , &dts )
4245
-
4246
- # Note: The other.day < ... condition will never hold when day_opt=='start'
4247
- # and the other.day > ... condition will never hold when day_opt=='end'.
4248
- # At some point these extra checks may need to be optimized away.
4249
- # But that point isn't today.
4250
- if n > 0:
4251
- if other.month < month or (other.month == month and
4252
- other.day < get_day_of_month(&dts ,
4253
- day_opt )):
4254
- n -= 1
4255
- else :
4256
- if other.month > month or (other.month == month and
4257
- other.day > get_day_of_month(& dts,
4258
- day_opt)):
4259
- n += 1
4260
- return n
0 commit comments