Skip to content

CLN/PERF: simplify tslib.get_time_micros #18389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 24, 2017
23 changes: 0 additions & 23 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions pandas/_libs/tslibs/fields.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we have this constant defined already somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, it’s defined in conversion. But it’s all the same to the compiler right? If it was something that was liable to change and needed to be kept in sync (eg nat_strings) it would make sense to cimport it. But for a constant like this it isn’t worth the hassle; it’d be like defining DAYSPERWEEK=7 and importing that all over the place.

return micros


def build_field_sarray(ndarray[int64_t] dtindex):
"""
Datetime as int64 representation to a structured array of fields
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down