Skip to content

Commit 559189a

Browse files
authored
REF: re-use month_offset (#35073)
1 parent f968883 commit 559189a

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

pandas/_libs/tslibs/ccalendar.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ cpdef int32_t get_day_of_year(int year, int month, int day) nogil
1414
cdef int64_t DAY_NANOS
1515
cdef int64_t HOUR_NANOS
1616
cdef dict c_MONTH_NUMBERS
17+
18+
cdef int32_t* month_offset

pandas/_libs/tslibs/ccalendar.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cdef int* sakamoto_arr = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
2727
# The first 13 entries give the month days elapsed as of the first of month N
2828
# (or the total number of days in the year for N=13) in non-leap years.
2929
# The remaining 13 entries give the days elapsed in leap years.
30-
cdef int32_t* _month_offset = [
30+
cdef int32_t* month_offset = [
3131
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365,
3232
0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]
3333

@@ -242,7 +242,7 @@ cpdef int32_t get_day_of_year(int year, int month, int day) nogil:
242242

243243
isleap = is_leapyear(year)
244244

245-
mo_off = _month_offset[isleap * 13 + month - 1]
245+
mo_off = month_offset[isleap * 13 + month - 1]
246246

247247
day_of_year = mo_off + day
248248
return day_of_year

pandas/_libs/tslibs/fields.pyx

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ from pandas._libs.tslibs.ccalendar import (
1717
from pandas._libs.tslibs.ccalendar cimport (
1818
DAY_NANOS,
1919
get_days_in_month, is_leapyear, dayofweek, get_week_of_year,
20-
get_day_of_year, get_iso_calendar, iso_calendar_t)
20+
get_day_of_year, get_iso_calendar, iso_calendar_t,
21+
month_offset,
22+
)
2123
from pandas._libs.tslibs.np_datetime cimport (
2224
npy_datetimestruct, pandas_timedeltastruct, dt64_to_dtstruct,
2325
td64_to_tdstruct)
@@ -155,19 +157,10 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
155157
int end_month = 12
156158
int start_month = 1
157159
ndarray[int8_t] out
158-
ndarray[int32_t, ndim=2] _month_offset
159160
bint isleap
160161
npy_datetimestruct dts
161162
int mo_off, dom, doy, dow, ldom
162163

163-
_month_offset = np.array(
164-
[
165-
[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365],
166-
[0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366],
167-
],
168-
dtype=np.int32,
169-
)
170-
171164
out = np.zeros(count, dtype='int8')
172165

173166
if freqstr:
@@ -226,10 +219,10 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
226219

227220
dt64_to_dtstruct(dtindex[i], &dts)
228221
isleap = is_leapyear(dts.year)
229-
mo_off = _month_offset[isleap, dts.month - 1]
222+
mo_off = month_offset[isleap * 13 + dts.month - 1]
230223
dom = dts.day
231224
doy = mo_off + dom
232-
ldom = _month_offset[isleap, dts.month]
225+
ldom = month_offset[isleap * 13 + dts.month]
233226
dow = dayofweek(dts.year, dts.month, dts.day)
234227

235228
if (ldom == doy and dow < 5) or (
@@ -244,10 +237,10 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
244237

245238
dt64_to_dtstruct(dtindex[i], &dts)
246239
isleap = is_leapyear(dts.year)
247-
mo_off = _month_offset[isleap, dts.month - 1]
240+
mo_off = month_offset[isleap * 13 + dts.month - 1]
248241
dom = dts.day
249242
doy = mo_off + dom
250-
ldom = _month_offset[isleap, dts.month]
243+
ldom = month_offset[isleap * 13 + dts.month]
251244

252245
if ldom == doy:
253246
out[i] = 1
@@ -288,10 +281,10 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
288281

289282
dt64_to_dtstruct(dtindex[i], &dts)
290283
isleap = is_leapyear(dts.year)
291-
mo_off = _month_offset[isleap, dts.month - 1]
284+
mo_off = month_offset[isleap * 13 + dts.month - 1]
292285
dom = dts.day
293286
doy = mo_off + dom
294-
ldom = _month_offset[isleap, dts.month]
287+
ldom = month_offset[isleap * 13 + dts.month]
295288
dow = dayofweek(dts.year, dts.month, dts.day)
296289

297290
if ((dts.month - end_month) % 3 == 0) and (
@@ -307,10 +300,10 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
307300

308301
dt64_to_dtstruct(dtindex[i], &dts)
309302
isleap = is_leapyear(dts.year)
310-
mo_off = _month_offset[isleap, dts.month - 1]
303+
mo_off = month_offset[isleap * 13 + dts.month - 1]
311304
dom = dts.day
312305
doy = mo_off + dom
313-
ldom = _month_offset[isleap, dts.month]
306+
ldom = month_offset[isleap * 13 + dts.month]
314307

315308
if ((dts.month - end_month) % 3 == 0) and (ldom == doy):
316309
out[i] = 1
@@ -352,10 +345,10 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
352345
dt64_to_dtstruct(dtindex[i], &dts)
353346
isleap = is_leapyear(dts.year)
354347
dom = dts.day
355-
mo_off = _month_offset[isleap, dts.month - 1]
348+
mo_off = month_offset[isleap * 13 + dts.month - 1]
356349
doy = mo_off + dom
357350
dow = dayofweek(dts.year, dts.month, dts.day)
358-
ldom = _month_offset[isleap, dts.month]
351+
ldom = month_offset[isleap * 13 + dts.month]
359352

360353
if (dts.month == end_month) and (
361354
(ldom == doy and dow < 5) or (
@@ -370,10 +363,10 @@ def get_start_end_field(const int64_t[:] dtindex, str field,
370363

371364
dt64_to_dtstruct(dtindex[i], &dts)
372365
isleap = is_leapyear(dts.year)
373-
mo_off = _month_offset[isleap, dts.month - 1]
366+
mo_off = month_offset[isleap * 13 + dts.month - 1]
374367
dom = dts.day
375368
doy = mo_off + dom
376-
ldom = _month_offset[isleap, dts.month]
369+
ldom = month_offset[isleap * 13 + dts.month]
377370

378371
if (dts.month == end_month) and (ldom == doy):
379372
out[i] = 1

0 commit comments

Comments
 (0)