Skip to content

Commit 67b1355

Browse files
committed
Merge pull request #11598 from lexual/issue_11433_slow_series_construct_no_values
PERF: Faster Series construction with no data and DatetimeIndex.
2 parents 10fe47e + 20511dd commit 67b1355

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
@@ -89,6 +89,7 @@ Performance Improvements
8989

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

9394
.. _whatsnew_0171.bug_fixes:
9495

pandas/core/series.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,22 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
172172
index = Index(_try_sort(data))
173173
try:
174174
if isinstance(index, DatetimeIndex):
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)
175+
if len(data):
176+
# coerce back to datetime objects for lookup
177+
data = _dict_compat(data)
178+
data = lib.fast_multiget(data, index.astype('O'),
179+
default=np.nan)
180+
else:
181+
data = np.nan
179182
elif isinstance(index, PeriodIndex):
180-
data = [data.get(i, nan) for i in index]
183+
data = [data.get(i, nan)
184+
for i in index] if data else np.nan
181185
else:
182186
data = lib.fast_multiget(data, index.values,
183187
default=np.nan)
184188
except TypeError:
185-
data = [data.get(i, nan) for i in index]
189+
data = [data.get(i, nan)
190+
for i in index] if data else np.nan
186191

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

0 commit comments

Comments
 (0)