Skip to content

Improve DatetimeIndex.time performance #18461

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 3 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asv_bench/benchmarks/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def time_dti_tz_factorize(self):
self.dti_tz.factorize()

def time_dti_time(self):
self.rng.time
self.dst_rng.time

def time_timestamp_tzinfo_cons(self):
self.rng5[0]
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ Performance Improvements
- Improved performance of :func:`Series.dt.date` and :func:`DatetimeIndex.date` (:issue:`18058`)
- Improved performance of :func:`IntervalIndex.symmetric_difference()` (:issue:`18475`)
- Improved performance of ``DatetimeIndex`` and ``Series`` arithmetic operations with Business-Month and Business-Quarter frequencies (:issue:`18489`)
- Improved performance of :func:`Series.dt.time` and :func:`DatetimeIndex.time`

.. _whatsnew_0220.docs:

Expand Down
21 changes: 15 additions & 6 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ from util cimport (is_integer_object, is_float_object, is_string_object,
from cpython.datetime cimport (PyDateTime_Check, PyDate_Check,
PyDateTime_CheckExact,
PyDateTime_IMPORT,
timedelta, datetime, date)
timedelta, datetime, date, time)
# import datetime C API
PyDateTime_IMPORT

Expand Down Expand Up @@ -70,11 +70,17 @@ cdef inline object create_date_from_ts(
""" convenience routine to construct a datetime.date from its parts """
return date(dts.year, dts.month, dts.day)

cdef inline object create_time_from_ts(
int64_t value, pandas_datetimestruct dts,
object tz, object freq):
""" convenience routine to construct a datetime.time from its parts """
return time(dts.hour, dts.min, dts.sec, dts.us, tz)


def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None,
box="datetime"):
"""
Convert an i8 repr to an ndarray of datetimes, date or Timestamp
Convert an i8 repr to an ndarray of datetimes, date, time or Timestamp

Parameters
----------
Expand All @@ -83,19 +89,17 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None,
convert to this timezone
freq : str/Offset, default None
freq to convert
box : {'datetime', 'timestamp', 'date'}, default 'datetime'
box : {'datetime', 'timestamp', 'date', 'time'}, default 'datetime'
If datetime, convert to datetime.datetime
If date, convert to datetime.date
If time, convert to datetime.time
If Timestamp, convert to pandas.Timestamp

Returns
-------
result : array of dtype specified by box
"""

assert ((box == "datetime") or (box == "date") or (box == "timestamp")), \
"box must be one of 'datetime', 'date' or 'timestamp'"

cdef:
Py_ssize_t i, n = len(arr)
ndarray[int64_t] trans, deltas
Expand All @@ -115,8 +119,13 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None,
if is_string_object(freq):
from pandas.tseries.frequencies import to_offset
freq = to_offset(freq)
elif box == "time":
func_create = create_time_from_ts
elif box == "datetime":
func_create = create_datetime_from_ts
Copy link
Contributor

Choose a reason for hiding this comment

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

alternatively, can remove the assert and make else the raise case

else:
raise ValueError("box must be one of 'datetime', 'date', 'time' or" +
" 'timestamp'")

if tz is not None:
if is_utc(tz):
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
import pandas.core.tools.datetimes as tools

from pandas._libs import (lib, index as libindex, tslib as libts,
algos as libalgos, join as libjoin,
Timestamp)
join as libjoin, Timestamp)
from pandas._libs.tslibs import (timezones, conversion, fields, parsing,
period as libperiod)

Expand Down Expand Up @@ -1677,9 +1676,7 @@ def time(self):
"""
Returns numpy array of datetime.time. The time part of the Timestamps.
"""
return self._maybe_mask_results(libalgos.arrmap_object(
self.astype(object).values,
lambda x: np.nan if x is libts.NaT else x.time()))
return libts.ints_to_pydatetime(self.asi8, self.tz, box="time")

@property
def date(self):
Expand Down