Skip to content

Commit aec3347

Browse files
jbrockmendeljreback
authored andcommitted
CLN/PERF: simplify tslib.get_time_micros (#18389)
1 parent de5faf1 commit aec3347

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

pandas/_libs/tslib.pyx

-23
Original file line numberDiff line numberDiff line change
@@ -730,29 +730,6 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
730730
return oresult
731731

732732

733-
# ----------------------------------------------------------------------
734-
# Accessors
735-
736-
737-
def get_time_micros(ndarray[int64_t] dtindex):
738-
"""
739-
Datetime as int64 representation to a structured array of fields
740-
"""
741-
cdef:
742-
Py_ssize_t i, n = len(dtindex)
743-
pandas_datetimestruct dts
744-
ndarray[int64_t] micros
745-
746-
micros = np.empty(n, dtype=np.int64)
747-
748-
for i in range(n):
749-
dt64_to_dtstruct(dtindex[i], &dts)
750-
micros[i] = 1000000LL * (dts.hour * 60 * 60 +
751-
60 * dts.min + dts.sec) + dts.us
752-
753-
return micros
754-
755-
756733
# ----------------------------------------------------------------------
757734
# Some general helper functions
758735

pandas/_libs/tslibs/fields.pyx

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ from np_datetime cimport (pandas_datetimestruct, pandas_timedeltastruct,
2323
from nattype cimport NPY_NAT
2424

2525

26+
def get_time_micros(ndarray[int64_t] dtindex):
27+
"""
28+
Return the number of microseconds in the time component of a
29+
nanosecond timestamp.
30+
31+
Parameters
32+
----------
33+
dtindex : ndarray[int64_t]
34+
35+
Returns
36+
-------
37+
micros : ndarray[int64_t]
38+
"""
39+
cdef:
40+
ndarray[int64_t] micros
41+
42+
micros = np.mod(dtindex, 86400000000000, dtype=np.int64) // 1000LL
43+
return micros
44+
45+
2646
def build_field_sarray(ndarray[int64_t] dtindex):
2747
"""
2848
Datetime as int64 representation to a structured array of fields

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ def _get_time_micros(self):
926926
values = self.asi8
927927
if self.tz is not None and self.tz is not utc:
928928
values = self._local_timestamps()
929-
return libts.get_time_micros(values)
929+
return fields.get_time_micros(values)
930930

931931
def to_series(self, keep_tz=False):
932932
"""

0 commit comments

Comments
 (0)