@@ -19,6 +19,8 @@ from pandas._libs.tslibs.ccalendar cimport (
19
19
get_days_in_month, is_leapyear, dayofweek, get_week_of_year,
20
20
get_day_of_year, get_iso_calendar, iso_calendar_t,
21
21
month_offset,
22
+ get_firstbday,
23
+ get_lastbday,
22
24
)
23
25
from pandas._libs.tslibs.np_datetime cimport (
24
26
npy_datetimestruct, pandas_timedeltastruct, dt64_to_dtstruct,
@@ -137,9 +139,7 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
137
139
int end_month = 12
138
140
int start_month = 1
139
141
ndarray[int8_t] out
140
- bint isleap
141
142
npy_datetimestruct dts
142
- int mo_off, dom, doy, dow, ldom
143
143
144
144
out = np.zeros(count, dtype = ' int8' )
145
145
@@ -172,10 +172,8 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
172
172
continue
173
173
174
174
dt64_to_dtstruct(dtindex[i], & dts)
175
- dom = dts.day
176
- dow = dayofweek(dts.year, dts.month, dts.day)
177
175
178
- if (dom == 1 and dow < 5 ) or (dom <= 3 and dow == 0 ):
176
+ if dts.day == get_firstbday(dts.year, dts.month ):
179
177
out[i] = 1
180
178
181
179
else :
@@ -185,9 +183,8 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
185
183
continue
186
184
187
185
dt64_to_dtstruct(dtindex[i], & dts)
188
- dom = dts.day
189
186
190
- if dom == 1 :
187
+ if dts.day == 1 :
191
188
out[i] = 1
192
189
193
190
elif field == ' is_month_end' :
@@ -198,15 +195,8 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
198
195
continue
199
196
200
197
dt64_to_dtstruct(dtindex[i], & dts)
201
- isleap = is_leapyear(dts.year)
202
- mo_off = month_offset[isleap * 13 + dts.month - 1 ]
203
- dom = dts.day
204
- doy = mo_off + dom
205
- ldom = month_offset[isleap * 13 + dts.month]
206
- dow = dayofweek(dts.year, dts.month, dts.day)
207
-
208
- if (ldom == doy and dow < 5 ) or (
209
- dow == 4 and (ldom - doy <= 2 )):
198
+
199
+ if dts.day == get_lastbday(dts.year, dts.month):
210
200
out[i] = 1
211
201
212
202
else :
@@ -216,13 +206,8 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
216
206
continue
217
207
218
208
dt64_to_dtstruct(dtindex[i], & dts)
219
- isleap = is_leapyear(dts.year)
220
- mo_off = month_offset[isleap * 13 + dts.month - 1 ]
221
- dom = dts.day
222
- doy = mo_off + dom
223
- ldom = month_offset[isleap * 13 + dts.month]
224
209
225
- if ldom == doy :
210
+ if dts.day == get_days_in_month(dts.year, dts.month) :
226
211
out[i] = 1
227
212
228
213
elif field == ' is_quarter_start' :
@@ -233,11 +218,9 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
233
218
continue
234
219
235
220
dt64_to_dtstruct(dtindex[i], & dts)
236
- dom = dts.day
237
- dow = dayofweek(dts.year, dts.month, dts.day)
238
221
239
222
if ((dts.month - start_month) % 3 == 0 ) and (
240
- (dom == 1 and dow < 5 ) or (dom <= 3 and dow == 0 )):
223
+ dts.day == get_firstbday(dts.year, dts.month )):
241
224
out[i] = 1
242
225
243
226
else :
@@ -247,9 +230,8 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
247
230
continue
248
231
249
232
dt64_to_dtstruct(dtindex[i], & dts)
250
- dom = dts.day
251
233
252
- if ((dts.month - start_month) % 3 == 0 ) and dom == 1 :
234
+ if ((dts.month - start_month) % 3 == 0 ) and dts.day == 1 :
253
235
out[i] = 1
254
236
255
237
elif field == ' is_quarter_end' :
@@ -260,16 +242,9 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
260
242
continue
261
243
262
244
dt64_to_dtstruct(dtindex[i], & dts)
263
- isleap = is_leapyear(dts.year)
264
- mo_off = month_offset[isleap * 13 + dts.month - 1 ]
265
- dom = dts.day
266
- doy = mo_off + dom
267
- ldom = month_offset[isleap * 13 + dts.month]
268
- dow = dayofweek(dts.year, dts.month, dts.day)
269
245
270
246
if ((dts.month - end_month) % 3 == 0 ) and (
271
- (ldom == doy and dow < 5 ) or (
272
- dow == 4 and (ldom - doy <= 2 ))):
247
+ dts.day == get_lastbday(dts.year, dts.month)):
273
248
out[i] = 1
274
249
275
250
else :
@@ -279,13 +254,9 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
279
254
continue
280
255
281
256
dt64_to_dtstruct(dtindex[i], & dts)
282
- isleap = is_leapyear(dts.year)
283
- mo_off = month_offset[isleap * 13 + dts.month - 1 ]
284
- dom = dts.day
285
- doy = mo_off + dom
286
- ldom = month_offset[isleap * 13 + dts.month]
287
257
288
- if ((dts.month - end_month) % 3 == 0 ) and (ldom == doy):
258
+ if ((dts.month - end_month) % 3 == 0 ) and (
259
+ dts.day == get_days_in_month(dts.year, dts.month)):
289
260
out[i] = 1
290
261
291
262
elif field == ' is_year_start' :
@@ -296,11 +267,9 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
296
267
continue
297
268
298
269
dt64_to_dtstruct(dtindex[i], & dts)
299
- dom = dts.day
300
- dow = dayofweek(dts.year, dts.month, dts.day)
301
270
302
271
if (dts.month == start_month) and (
303
- (dom == 1 and dow < 5 ) or (dom <= 3 and dow == 0 )):
272
+ dts.day == get_firstbday(dts.year, dts.month )):
304
273
out[i] = 1
305
274
306
275
else :
@@ -310,9 +279,8 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
310
279
continue
311
280
312
281
dt64_to_dtstruct(dtindex[i], & dts)
313
- dom = dts.day
314
282
315
- if (dts.month == start_month) and dom == 1 :
283
+ if (dts.month == start_month) and dts.day == 1 :
316
284
out[i] = 1
317
285
318
286
elif field == ' is_year_end' :
@@ -323,16 +291,9 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
323
291
continue
324
292
325
293
dt64_to_dtstruct(dtindex[i], & dts)
326
- isleap = is_leapyear(dts.year)
327
- dom = dts.day
328
- mo_off = month_offset[isleap * 13 + dts.month - 1 ]
329
- doy = mo_off + dom
330
- dow = dayofweek(dts.year, dts.month, dts.day)
331
- ldom = month_offset[isleap * 13 + dts.month]
332
294
333
295
if (dts.month == end_month) and (
334
- (ldom == doy and dow < 5 ) or (
335
- dow == 4 and (ldom - doy <= 2 ))):
296
+ dts.day == get_lastbday(dts.year, dts.month)):
336
297
out[i] = 1
337
298
338
299
else :
@@ -342,13 +303,9 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
342
303
continue
343
304
344
305
dt64_to_dtstruct(dtindex[i], & dts)
345
- isleap = is_leapyear(dts.year)
346
- mo_off = month_offset[isleap * 13 + dts.month - 1 ]
347
- dom = dts.day
348
- doy = mo_off + dom
349
- ldom = month_offset[isleap * 13 + dts.month]
350
306
351
- if (dts.month == end_month) and (ldom == doy):
307
+ if (dts.month == end_month) and (
308
+ dts.day == get_days_in_month(dts.year, dts.month)):
352
309
out[i] = 1
353
310
354
311
else :
0 commit comments