Skip to content

Commit 20511dd

Browse files
committed
PERF: Faster Series construction with no data and DatetimeIndex.
ref #11433 Code. taken from @jreback comment on #11433
1 parent 83795a3 commit 20511dd

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

asv_bench/benchmarks/series_methods.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
from .pandas_vb_common import *
22

33

4+
class series_constructor_no_data_datetime_index(object):
5+
goal_time = 0.2
6+
7+
def setup(self):
8+
self.dr = pd.date_range(
9+
start=datetime(2015,10,26),
10+
end=datetime(2016,1,1),
11+
freq='10s'
12+
) # ~500k long
13+
14+
def time_series_constructor_no_data_datetime_index(self):
15+
Series(data=None, index=self.dr)
16+
17+
418
class series_isin_int64(object):
519
goal_time = 0.2
620

doc/source/whatsnew/v0.17.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Performance Improvements
8686

8787
- Improved performance to ``to_excel`` (:issue:`11352`)
8888
- Performance bug in repr of ``Categorical`` categories, which was rendering the strings before chopping them for display (:issue:`11305`)
89+
- Improved performance of ``Series`` constructor with no data and ``DatetimeIndex`` (:issue:`11433`)
8990

9091
.. _whatsnew_0171.bug_fixes:
9192

pandas/core/series.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,22 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
171171
index = Index(_try_sort(data))
172172
try:
173173
if isinstance(index, DatetimeIndex):
174-
# coerce back to datetime objects for lookup
175-
data = _dict_compat(data)
176-
data = lib.fast_multiget(data, index.astype('O'),
177-
default=np.nan)
174+
if len(data):
175+
# coerce back to datetime objects for lookup
176+
data = _dict_compat(data)
177+
data = lib.fast_multiget(data, index.astype('O'),
178+
default=np.nan)
179+
else:
180+
data = np.nan
178181
elif isinstance(index, PeriodIndex):
179-
data = [data.get(i, nan) for i in index]
182+
data = [data.get(i, nan)
183+
for i in index] if data else np.nan
180184
else:
181185
data = lib.fast_multiget(data, index.values,
182186
default=np.nan)
183187
except TypeError:
184-
data = [data.get(i, nan) for i in index]
188+
data = [data.get(i, nan)
189+
for i in index] if data else np.nan
185190

186191
elif isinstance(data, SingleBlockManager):
187192
if index is None:

0 commit comments

Comments
 (0)