From c09371d1ef9662802f8f681936c2fb93d2d5d6a1 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 29 Jun 2020 12:30:46 -0700 Subject: [PATCH] REF: re-use month_offset --- pandas/_libs/tslibs/ccalendar.pxd | 2 ++ pandas/_libs/tslibs/ccalendar.pyx | 4 ++-- pandas/_libs/tslibs/fields.pyx | 37 +++++++++++++------------------ 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/pandas/_libs/tslibs/ccalendar.pxd b/pandas/_libs/tslibs/ccalendar.pxd index b55780fe7d5b9..41cc477413607 100644 --- a/pandas/_libs/tslibs/ccalendar.pxd +++ b/pandas/_libs/tslibs/ccalendar.pxd @@ -14,3 +14,5 @@ cpdef int32_t get_day_of_year(int year, int month, int day) nogil cdef int64_t DAY_NANOS cdef int64_t HOUR_NANOS cdef dict c_MONTH_NUMBERS + +cdef int32_t* month_offset diff --git a/pandas/_libs/tslibs/ccalendar.pyx b/pandas/_libs/tslibs/ccalendar.pyx index 2006214169a74..9f8cf6c28adab 100644 --- a/pandas/_libs/tslibs/ccalendar.pyx +++ b/pandas/_libs/tslibs/ccalendar.pyx @@ -27,7 +27,7 @@ cdef int* sakamoto_arr = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4] # The first 13 entries give the month days elapsed as of the first of month N # (or the total number of days in the year for N=13) in non-leap years. # The remaining 13 entries give the days elapsed in leap years. -cdef int32_t* _month_offset = [ +cdef int32_t* month_offset = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] @@ -242,7 +242,7 @@ cpdef int32_t get_day_of_year(int year, int month, int day) nogil: isleap = is_leapyear(year) - mo_off = _month_offset[isleap * 13 + month - 1] + mo_off = month_offset[isleap * 13 + month - 1] day_of_year = mo_off + day return day_of_year diff --git a/pandas/_libs/tslibs/fields.pyx b/pandas/_libs/tslibs/fields.pyx index 8d83eeb011866..ca6cd056a48f7 100644 --- a/pandas/_libs/tslibs/fields.pyx +++ b/pandas/_libs/tslibs/fields.pyx @@ -17,7 +17,9 @@ from pandas._libs.tslibs.ccalendar import ( from pandas._libs.tslibs.ccalendar cimport ( DAY_NANOS, get_days_in_month, is_leapyear, dayofweek, get_week_of_year, - get_day_of_year, get_iso_calendar, iso_calendar_t) + get_day_of_year, get_iso_calendar, iso_calendar_t, + month_offset, +) from pandas._libs.tslibs.np_datetime cimport ( npy_datetimestruct, pandas_timedeltastruct, dt64_to_dtstruct, td64_to_tdstruct) @@ -155,19 +157,10 @@ def get_start_end_field(const int64_t[:] dtindex, object field, int end_month = 12 int start_month = 1 ndarray[int8_t] out - ndarray[int32_t, ndim=2] _month_offset bint isleap npy_datetimestruct dts int mo_off, dom, doy, dow, ldom - _month_offset = np.array( - [ - [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365], - [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366], - ], - dtype=np.int32, - ) - out = np.zeros(count, dtype='int8') if freqstr: @@ -226,10 +219,10 @@ def get_start_end_field(const int64_t[:] dtindex, object field, dt64_to_dtstruct(dtindex[i], &dts) isleap = is_leapyear(dts.year) - mo_off = _month_offset[isleap, dts.month - 1] + mo_off = month_offset[isleap * 13 + dts.month - 1] dom = dts.day doy = mo_off + dom - ldom = _month_offset[isleap, dts.month] + ldom = month_offset[isleap * 13 + dts.month] dow = dayofweek(dts.year, dts.month, dts.day) if (ldom == doy and dow < 5) or ( @@ -244,10 +237,10 @@ def get_start_end_field(const int64_t[:] dtindex, object field, dt64_to_dtstruct(dtindex[i], &dts) isleap = is_leapyear(dts.year) - mo_off = _month_offset[isleap, dts.month - 1] + mo_off = month_offset[isleap * 13 + dts.month - 1] dom = dts.day doy = mo_off + dom - ldom = _month_offset[isleap, dts.month] + ldom = month_offset[isleap * 13 + dts.month] if ldom == doy: out[i] = 1 @@ -288,10 +281,10 @@ def get_start_end_field(const int64_t[:] dtindex, object field, dt64_to_dtstruct(dtindex[i], &dts) isleap = is_leapyear(dts.year) - mo_off = _month_offset[isleap, dts.month - 1] + mo_off = month_offset[isleap * 13 + dts.month - 1] dom = dts.day doy = mo_off + dom - ldom = _month_offset[isleap, dts.month] + ldom = month_offset[isleap * 13 + dts.month] dow = dayofweek(dts.year, dts.month, dts.day) if ((dts.month - end_month) % 3 == 0) and ( @@ -307,10 +300,10 @@ def get_start_end_field(const int64_t[:] dtindex, object field, dt64_to_dtstruct(dtindex[i], &dts) isleap = is_leapyear(dts.year) - mo_off = _month_offset[isleap, dts.month - 1] + mo_off = month_offset[isleap * 13 + dts.month - 1] dom = dts.day doy = mo_off + dom - ldom = _month_offset[isleap, dts.month] + ldom = month_offset[isleap * 13 + dts.month] if ((dts.month - end_month) % 3 == 0) and (ldom == doy): out[i] = 1 @@ -352,10 +345,10 @@ def get_start_end_field(const int64_t[:] dtindex, object field, dt64_to_dtstruct(dtindex[i], &dts) isleap = is_leapyear(dts.year) dom = dts.day - mo_off = _month_offset[isleap, dts.month - 1] + mo_off = month_offset[isleap * 13 + dts.month - 1] doy = mo_off + dom dow = dayofweek(dts.year, dts.month, dts.day) - ldom = _month_offset[isleap, dts.month] + ldom = month_offset[isleap * 13 + dts.month] if (dts.month == end_month) and ( (ldom == doy and dow < 5) or ( @@ -370,10 +363,10 @@ def get_start_end_field(const int64_t[:] dtindex, object field, dt64_to_dtstruct(dtindex[i], &dts) isleap = is_leapyear(dts.year) - mo_off = _month_offset[isleap, dts.month - 1] + mo_off = month_offset[isleap * 13 + dts.month - 1] dom = dts.day doy = mo_off + dom - ldom = _month_offset[isleap, dts.month] + ldom = month_offset[isleap * 13 + dts.month] if (dts.month == end_month) and (ldom == doy): out[i] = 1