Skip to content
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

ENH/PERF: Remove frequency inference from .dt accessor #17210

Merged
merged 3 commits into from
Aug 14, 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
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