From afabf8e6dac5116929b7ea35584453ef69524750 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 19 Jun 2018 16:07:06 -0700 Subject: [PATCH 1/2] use ccalendar instead of np_datetime --- pandas/_libs/src/datetime/np_datetime.c | 12 ------------ pandas/_libs/src/datetime/np_datetime.h | 4 ---- pandas/_libs/tslibs/np_datetime.pxd | 4 ---- pandas/_libs/tslibs/offsets.pyx | 10 ++-------- setup.py | 1 + 5 files changed, 3 insertions(+), 28 deletions(-) diff --git a/pandas/_libs/src/datetime/np_datetime.c b/pandas/_libs/src/datetime/np_datetime.c index 89753ccf7d773..9f6fa38d2c37e 100644 --- a/pandas/_libs/src/datetime/np_datetime.c +++ b/pandas/_libs/src/datetime/np_datetime.c @@ -45,18 +45,6 @@ int is_leapyear(npy_int64 year) { ((year % 100) != 0 || (year % 400) == 0); } -/* - * Sakamoto's method, from wikipedia - */ -int dayofweek(int y, int m, int d) { - int day; - static const int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; - y -= m < 3; - day = (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7; - // convert to python day - return (day + 6) % 7; -} - /* * Adjusts a datetimestruct based on a minutes offset. Assumes * the current values are valid.g diff --git a/pandas/_libs/src/datetime/np_datetime.h b/pandas/_libs/src/datetime/np_datetime.h index b6c0852bfe764..cb06fe57e295f 100644 --- a/pandas/_libs/src/datetime/np_datetime.h +++ b/pandas/_libs/src/datetime/np_datetime.h @@ -71,15 +71,11 @@ void pandas_timedelta_to_timedeltastruct(npy_timedelta val, PANDAS_DATETIMEUNIT fr, pandas_timedeltastruct *result); -int dayofweek(int y, int m, int d); - extern const int days_per_month_table[2][12]; // stuff numpy-derived code needs in header // ---------------------------------------------------------------------------- -int is_leapyear(npy_int64 year); - /* * Calculates the days offset from the 1970 epoch. */ diff --git a/pandas/_libs/tslibs/np_datetime.pxd b/pandas/_libs/tslibs/np_datetime.pxd index 33b8b32bcf2dc..1a0baa8271643 100644 --- a/pandas/_libs/tslibs/np_datetime.pxd +++ b/pandas/_libs/tslibs/np_datetime.pxd @@ -54,10 +54,6 @@ cdef extern from "../src/datetime/np_datetime.h": PANDAS_DATETIMEUNIT fr, pandas_datetimestruct *result) nogil - int days_per_month_table[2][12] - int dayofweek(int y, int m, int d) nogil - int is_leapyear(int64_t year) nogil - cdef int reverse_ops[6] diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 3ca9bb307da9c..3c15414c34b55 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -18,12 +18,12 @@ cnp.import_array() from util cimport is_string_object, is_integer_object from ccalendar import MONTHS, DAYS +from ccalendar cimport get_days_in_month, dayofweek from conversion cimport tz_convert_single, pydt_to_i8 from frequencies cimport get_freq_code from nattype cimport NPY_NAT from np_datetime cimport (pandas_datetimestruct, - dtstruct_to_dt64, dt64_to_dtstruct, - is_leapyear, days_per_month_table, dayofweek) + dtstruct_to_dt64, dt64_to_dtstruct) # --------------------------------------------------------------------- # Constants @@ -437,12 +437,6 @@ class BaseOffset(_BaseOffset): # ---------------------------------------------------------------------- # RelativeDelta Arithmetic -@cython.wraparound(False) -@cython.boundscheck(False) -cdef inline int get_days_in_month(int year, int month) nogil: - return days_per_month_table[is_leapyear(year)][month - 1] - - cdef inline int year_add_months(pandas_datetimestruct dts, int months) nogil: """new year number after shifting pandas_datetimestruct number of months""" return dts.year + (dts.month + months - 1) / 12 diff --git a/setup.py b/setup.py index d6890a08b09d0..b0b377ecc9e0f 100755 --- a/setup.py +++ b/setup.py @@ -591,6 +591,7 @@ def pxd(name): '_libs.tslibs.offsets': { 'pyxfile': '_libs/tslibs/offsets', 'pxdfiles': ['_libs/src/util', + '_libs/tslibs/ccalendar', '_libs/tslibs/conversion', '_libs/tslibs/frequencies', '_libs/tslibs/nattype'], From f986a5c6cca16ce81be1682b799cfa5cbc7091c4 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 23 Jun 2018 20:22:56 -0700 Subject: [PATCH 2/2] restore definitions needed by period_helper --- pandas/_libs/src/datetime/np_datetime.c | 12 ++++++++++++ pandas/_libs/src/datetime/np_datetime.h | 4 ++++ pandas/_libs/tslibs/ccalendar.pxd | 2 +- pandas/_libs/tslibs/period.pyx | 3 +-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/src/datetime/np_datetime.c b/pandas/_libs/src/datetime/np_datetime.c index 9f6fa38d2c37e..89753ccf7d773 100644 --- a/pandas/_libs/src/datetime/np_datetime.c +++ b/pandas/_libs/src/datetime/np_datetime.c @@ -45,6 +45,18 @@ int is_leapyear(npy_int64 year) { ((year % 100) != 0 || (year % 400) == 0); } +/* + * Sakamoto's method, from wikipedia + */ +int dayofweek(int y, int m, int d) { + int day; + static const int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; + y -= m < 3; + day = (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7; + // convert to python day + return (day + 6) % 7; +} + /* * Adjusts a datetimestruct based on a minutes offset. Assumes * the current values are valid.g diff --git a/pandas/_libs/src/datetime/np_datetime.h b/pandas/_libs/src/datetime/np_datetime.h index cb06fe57e295f..b6c0852bfe764 100644 --- a/pandas/_libs/src/datetime/np_datetime.h +++ b/pandas/_libs/src/datetime/np_datetime.h @@ -71,11 +71,15 @@ void pandas_timedelta_to_timedeltastruct(npy_timedelta val, PANDAS_DATETIMEUNIT fr, pandas_timedeltastruct *result); +int dayofweek(int y, int m, int d); + extern const int days_per_month_table[2][12]; // stuff numpy-derived code needs in header // ---------------------------------------------------------------------------- +int is_leapyear(npy_int64 year); + /* * Calculates the days offset from the 1970 epoch. */ diff --git a/pandas/_libs/tslibs/ccalendar.pxd b/pandas/_libs/tslibs/ccalendar.pxd index 42473a97a7150..04fb6eaf49c84 100644 --- a/pandas/_libs/tslibs/ccalendar.pxd +++ b/pandas/_libs/tslibs/ccalendar.pxd @@ -6,7 +6,7 @@ from cython cimport Py_ssize_t from numpy cimport int64_t, int32_t -cdef int dayofweek(int y, int m, int m) nogil +cdef int dayofweek(int y, int m, int d) nogil cdef bint is_leapyear(int64_t year) nogil cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil cpdef int32_t get_week_of_year(int year, int month, int day) nogil diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index cc2fb6e0617cb..49208056f88fe 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -45,9 +45,8 @@ from timezones cimport is_utc, is_tzlocal, get_utcoffset, get_dst_info from timedeltas cimport delta_to_nanoseconds cimport ccalendar -from ccalendar cimport dayofweek, get_day_of_year +from ccalendar cimport dayofweek, get_day_of_year, is_leapyear from ccalendar import MONTH_NUMBERS -from ccalendar cimport is_leapyear from conversion cimport tz_convert_utc_to_tzlocal from frequencies cimport (get_freq_code, get_base_alias, get_to_timestamp_base, get_freq_str,