From 383e8402519dd06009efb75eb780df098d093b32 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 26 May 2020 13:09:27 -0700 Subject: [PATCH 1/3] CLN: de-duplicate, annotate --- pandas/_libs/tslibs/parsing.pyx | 2 +- pandas/_libs/tslibs/period.pyx | 17 +---------------- pandas/_libs/tslibs/resolution.pyx | 16 ++++------------ 3 files changed, 6 insertions(+), 29 deletions(-) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index bf895f155fc59..3a1af9fdb1e8f 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -376,7 +376,7 @@ cpdef bint _does_string_look_like_datetime(str py_string): return True -cdef inline object _parse_dateabbr_string(object date_string, object default, +cdef inline object _parse_dateabbr_string(object date_string, datetime default, object freq): cdef: object ret diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index cc6c4d06ae562..e9d9830c5fd93 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -964,23 +964,8 @@ def dt64arr_to_periodarr(const int64_t[:] dtarr, int freq, tz=None): """ cdef: int64_t[:] out - Py_ssize_t i, l - npy_datetimestruct dts - - l = len(dtarr) - - out = np.empty(l, dtype='i8') - if tz is None: - with nogil: - for i in range(l): - if dtarr[i] == NPY_NAT: - out[i] = NPY_NAT - continue - dt64_to_dtstruct(dtarr[i], &dts) - out[i] = get_period_ordinal(&dts, freq) - else: - out = localize_dt64arr_to_period(dtarr, freq, tz) + out = localize_dt64arr_to_period(dtarr, freq, tz) return out.base # .base to access underlying np.ndarray diff --git a/pandas/_libs/tslibs/resolution.pyx b/pandas/_libs/tslibs/resolution.pyx index 3d76483f76600..2133573ee7554 100644 --- a/pandas/_libs/tslibs/resolution.pyx +++ b/pandas/_libs/tslibs/resolution.pyx @@ -28,27 +28,19 @@ cdef: # ---------------------------------------------------------------------- -cpdef resolution(const int64_t[:] stamps, tz=None): +def resolution(const int64_t[:] stamps, tz=None): cdef: Py_ssize_t i, n = len(stamps) npy_datetimestruct dts int reso = RESO_DAY, curr_reso - - if tz is not None: - tz = maybe_get_tz(tz) - return _reso_local(stamps, tz) - - -cdef _reso_local(const int64_t[:] stamps, object tz): - cdef: - Py_ssize_t i, n = len(stamps) - int reso = RESO_DAY, curr_reso ndarray[int64_t] trans int64_t[:] deltas Py_ssize_t[:] pos - npy_datetimestruct dts int64_t local_val, delta + if tz is not None: + tz = maybe_get_tz(tz) + if is_utc(tz) or tz is None: for i in range(n): if stamps[i] == NPY_NAT: From 006956038ec5955d2b60f24c8e073c830340940a Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 26 May 2020 20:22:18 -0700 Subject: [PATCH 2/3] lint fixup --- pandas/tseries/offsets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index c5de4533955ff..fd304dc2425a3 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -1,5 +1,4 @@ from datetime import datetime, timedelta -import operator import numpy as np From 6e3530df42a971cb975ead2c97a29f0b6f04f854 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 27 May 2020 08:20:05 -0700 Subject: [PATCH 3/3] remove unnecessary shim call --- pandas/_libs/tslibs/period.pyx | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index e9d9830c5fd93..b5488e3d6d5a3 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -955,20 +955,6 @@ cdef inline int month_to_quarter(int month) nogil: # ---------------------------------------------------------------------- # Period logic -@cython.wraparound(False) -@cython.boundscheck(False) -def dt64arr_to_periodarr(const int64_t[:] dtarr, int freq, tz=None): - """ - Convert array of datetime64 values (passed in as 'i8' dtype) to a set of - periods corresponding to desired frequency, per period convention. - """ - cdef: - int64_t[:] out - - out = localize_dt64arr_to_period(dtarr, freq, tz) - return out.base # .base to access underlying np.ndarray - - @cython.wraparound(False) @cython.boundscheck(False) def periodarr_to_dt64arr(const int64_t[:] periodarr, int freq): @@ -1456,8 +1442,7 @@ def extract_freq(ndarray[object] values): @cython.wraparound(False) @cython.boundscheck(False) -cdef int64_t[:] localize_dt64arr_to_period(const int64_t[:] stamps, - int freq, object tz): +def dt64arr_to_periodarr(const int64_t[:] stamps, int freq, object tz): cdef: Py_ssize_t n = len(stamps) int64_t[:] result = np.empty(n, dtype=np.int64) @@ -1506,7 +1491,7 @@ cdef int64_t[:] localize_dt64arr_to_period(const int64_t[:] stamps, dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts) result[i] = get_period_ordinal(&dts, freq) - return result + return result.base # .base to get underlying ndarray DIFFERENT_FREQ = ("Input has different freq={other_freq} "