From aec33479b923a05ead1ca35335f00aba87e4145e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 24 Nov 2017 08:59:12 -0800 Subject: [PATCH] CLN/PERF: simplify tslib.get_time_micros (#18389) --- pandas/_libs/tslib.pyx | 23 ----------------------- pandas/_libs/tslibs/fields.pyx | 20 ++++++++++++++++++++ pandas/core/indexes/datetimes.py | 2 +- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index ea4f4728a0741..2c43bed4ad053 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -730,29 +730,6 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise', return oresult -# ---------------------------------------------------------------------- -# Accessors - - -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 - - 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): """