From fd8718b837cd8d590728a919fbac077d0402ef79 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 20 Nov 2017 08:20:26 -0800 Subject: [PATCH 1/4] simplify get_time_micros --- pandas/_libs/tslib.pyx | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 705336dfadf90..f0d334b7d9781 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -1704,17 +1704,9 @@ def get_time_micros(ndarray[int64_t] dtindex): Datetime as int64 representation to a structured array of fields """ cdef: - Py_ssize_t i, n = len(dtindex) - pandas_datetimestruct dts ndarray[int64_t] micros - micros = np.empty(n, dtype=np.int64) - - for i in range(n): - dt64_to_dtstruct(dtindex[i], &dts) - micros[i] = 1000000LL * (dts.hour * 60 * 60 + - 60 * dts.min + dts.sec) + dts.us - + micros = np.mod(dtindex, 86_400_000_000_000, dtype=np.int64) // 1000LL return micros From e7c85bd6e175225ce23d5f2785e8fba6a90eddec Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 20 Nov 2017 08:21:53 -0800 Subject: [PATCH 2/4] rewrite inaccurate docstring --- pandas/_libs/tslib.pyx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index f0d334b7d9781..e0b58e03c7ce1 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -1701,7 +1701,16 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise', def get_time_micros(ndarray[int64_t] dtindex): """ - Datetime as int64 representation to a structured array of fields + Return the number of microseconds in the time component of a + nanosecond timestamp. + + Parameters + ---------- + dtindex : ndarray[int64_t] + + Returns + ------- + micros : ndarray[int64_t] """ cdef: ndarray[int64_t] micros From d0af10951819f200b3dd9a8536af33998d869dd0 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 20 Nov 2017 12:33:28 -0800 Subject: [PATCH 3/4] use py2-friendly syntax --- pandas/_libs/tslib.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index e0b58e03c7ce1..77f201ff083c7 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -1715,7 +1715,7 @@ def get_time_micros(ndarray[int64_t] dtindex): cdef: ndarray[int64_t] micros - micros = np.mod(dtindex, 86_400_000_000_000, dtype=np.int64) // 1000LL + micros = np.mod(dtindex, 86400000000000, dtype=np.int64) // 1000LL return micros From b71156932355c31b50524c06989e24259c8647a1 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Thu, 23 Nov 2017 08:39:34 -0800 Subject: [PATCH 4/4] move get_time_micros to tslibs.fields; update imports --- pandas/_libs/tslib.pyx | 24 ------------------------ pandas/_libs/tslibs/fields.pyx | 20 ++++++++++++++++++++ pandas/core/indexes/datetimes.py | 2 +- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 9fce53213fc00..2c43bed4ad053 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -730,30 +730,6 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise', return oresult -# ---------------------------------------------------------------------- -# Accessors - - -def get_time_micros(ndarray[int64_t] dtindex): - """ - Return the number of microseconds in the time component of a - nanosecond timestamp. - - Parameters - ---------- - dtindex : ndarray[int64_t] - - Returns - ------- - micros : ndarray[int64_t] - """ - cdef: - ndarray[int64_t] micros - - micros = np.mod(dtindex, 86400000000000, dtype=np.int64) // 1000LL - return micros - - # ---------------------------------------------------------------------- # Some general helper functions diff --git a/pandas/_libs/tslibs/fields.pyx b/pandas/_libs/tslibs/fields.pyx index e813fad1d3fa7..3de361c511fbf 100644 --- a/pandas/_libs/tslibs/fields.pyx +++ b/pandas/_libs/tslibs/fields.pyx @@ -23,6 +23,26 @@ from np_datetime cimport (pandas_datetimestruct, pandas_timedeltastruct, from nattype cimport NPY_NAT +def get_time_micros(ndarray[int64_t] dtindex): + """ + Return the number of microseconds in the time component of a + nanosecond timestamp. + + Parameters + ---------- + dtindex : ndarray[int64_t] + + Returns + ------- + micros : ndarray[int64_t] + """ + cdef: + ndarray[int64_t] micros + + micros = np.mod(dtindex, 86400000000000, dtype=np.int64) // 1000LL + return micros + + def build_field_sarray(ndarray[int64_t] dtindex): """ Datetime as int64 representation to a structured array of fields diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index e08bf4a625bce..111ba0c92aa9b 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -926,7 +926,7 @@ def _get_time_micros(self): values = self.asi8 if self.tz is not None and self.tz is not utc: values = self._local_timestamps() - return libts.get_time_micros(values) + return fields.get_time_micros(values) def to_series(self, keep_tz=False): """