Skip to content

Commit a2f044b

Browse files
cpcloudalanbato
authored andcommitted
ENH/PERF: Remove frequency inference from .dt accessor (pandas-dev#17210)
* ENH/PERF: Remove frequency inference from .dt accessor * BENCH: Add DatetimeAccessor benchmark * DOC: Whatsnew
1 parent ac66cae commit a2f044b

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

asv_bench/benchmarks/timeseries.py

+14
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,17 @@ def time_begin_incr_rng(self):
510510

511511
def time_begin_decr_rng(self):
512512
self.rng - self.semi_month_begin
513+
514+
515+
class DatetimeAccessor(object):
516+
def setup(self):
517+
self.N = 100000
518+
self.series = pd.Series(
519+
pd.date_range(start='1/1/2000', periods=self.N, freq='T')
520+
)
521+
522+
def time_dt_accessor(self):
523+
self.series.dt
524+
525+
def time_dt_accessor_normalize(self):
526+
self.series.dt.normalize()

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ Performance Improvements
296296
~~~~~~~~~~~~~~~~~~~~~~~~
297297

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

300301

301302
.. _whatsnew_0210.bug_fixes:

pandas/core/indexes/accessors.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,20 @@ def maybe_to_datetimelike(data, copy=False):
6161
data = orig.values.categories
6262

6363
if is_datetime64_dtype(data.dtype):
64-
return DatetimeProperties(DatetimeIndex(data, copy=copy, freq='infer'),
64+
return DatetimeProperties(DatetimeIndex(data, copy=copy),
6565
index, name=name, orig=orig)
6666
elif is_datetime64tz_dtype(data.dtype):
67-
return DatetimeProperties(DatetimeIndex(data, copy=copy, freq='infer',
68-
ambiguous='infer'),
67+
return DatetimeProperties(DatetimeIndex(data, copy=copy),
6968
index, data.name, orig=orig)
7069
elif is_timedelta64_dtype(data.dtype):
71-
return TimedeltaProperties(TimedeltaIndex(data, copy=copy,
72-
freq='infer'), index,
70+
return TimedeltaProperties(TimedeltaIndex(data, copy=copy), index,
7371
name=name, orig=orig)
7472
else:
7573
if is_period_arraylike(data):
7674
return PeriodProperties(PeriodIndex(data, copy=copy), index,
7775
name=name, orig=orig)
7876
if is_datetime_arraylike(data):
79-
return DatetimeProperties(DatetimeIndex(data, copy=copy,
80-
freq='infer'), index,
77+
return DatetimeProperties(DatetimeIndex(data, copy=copy), index,
8178
name=name, orig=orig)
8279

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

162+
@property
163+
def freq(self):
164+
return self.values.inferred_freq
165+
165166

166167
DatetimeProperties._add_delegate_accessors(
167168
delegate=DatetimeIndex,
@@ -202,6 +203,10 @@ def components(self):
202203
"""
203204
return self.values.components.set_index(self.index)
204205

206+
@property
207+
def freq(self):
208+
return self.values.inferred_freq
209+
205210

206211
TimedeltaProperties._add_delegate_accessors(
207212
delegate=TimedeltaIndex,

0 commit comments

Comments
 (0)