Skip to content

Commit

Permalink
ENH/PERF: Remove frequency inference from .dt accessor (#17210)
Browse files Browse the repository at this point in the history
* ENH/PERF: Remove frequency inference from .dt accessor

* BENCH: Add DatetimeAccessor benchmark

* DOC: Whatsnew
  • Loading branch information
cpcloud authored and jreback committed Aug 14, 2017
1 parent 330b8c1 commit 0fafd4f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
14 changes: 14 additions & 0 deletions asv_bench/benchmarks/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,17 @@ def time_begin_incr_rng(self):

def time_begin_decr_rng(self):
self.rng - self.semi_month_begin


class DatetimeAccessor(object):
def setup(self):
self.N = 100000
self.series = pd.Series(
pd.date_range(start='1/1/2000', periods=self.N, freq='T')
)

def time_dt_accessor(self):
self.series.dt

def time_dt_accessor_normalize(self):
self.series.dt.normalize()
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~

- Improved performance of instantiating :class:`SparseDataFrame` (:issue:`16773`)
- :attr:`Series.dt` no longer performs frequency inference, yielding a large speedup when accessing the attribute (:issue:`17210`)


.. _whatsnew_0210.bug_fixes:
Expand Down
19 changes: 12 additions & 7 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,20 @@ def maybe_to_datetimelike(data, copy=False):
data = orig.values.categories

if is_datetime64_dtype(data.dtype):
return DatetimeProperties(DatetimeIndex(data, copy=copy, freq='infer'),
return DatetimeProperties(DatetimeIndex(data, copy=copy),
index, name=name, orig=orig)
elif is_datetime64tz_dtype(data.dtype):
return DatetimeProperties(DatetimeIndex(data, copy=copy, freq='infer',
ambiguous='infer'),
return DatetimeProperties(DatetimeIndex(data, copy=copy),
index, data.name, orig=orig)
elif is_timedelta64_dtype(data.dtype):
return TimedeltaProperties(TimedeltaIndex(data, copy=copy,
freq='infer'), index,
return TimedeltaProperties(TimedeltaIndex(data, copy=copy), index,
name=name, orig=orig)
else:
if is_period_arraylike(data):
return PeriodProperties(PeriodIndex(data, copy=copy), index,
name=name, orig=orig)
if is_datetime_arraylike(data):
return DatetimeProperties(DatetimeIndex(data, copy=copy,
freq='infer'), index,
return DatetimeProperties(DatetimeIndex(data, copy=copy), index,
name=name, orig=orig)

raise TypeError("cannot convert an object of type {0} to a "
Expand Down Expand Up @@ -162,6 +159,10 @@ class DatetimeProperties(Properties):
def to_pydatetime(self):
return self.values.to_pydatetime()

@property
def freq(self):
return self.values.inferred_freq


DatetimeProperties._add_delegate_accessors(
delegate=DatetimeIndex,
Expand Down Expand Up @@ -202,6 +203,10 @@ def components(self):
"""
return self.values.components.set_index(self.index)

@property
def freq(self):
return self.values.inferred_freq


TimedeltaProperties._add_delegate_accessors(
delegate=TimedeltaIndex,
Expand Down

0 comments on commit 0fafd4f

Please sign in to comment.