Skip to content

Commit

Permalink
PERF: Fixed regression in Series(index=idx) constructor
Browse files Browse the repository at this point in the history
From pandas-dev#18496

Special cases empty series construction, since the reindex is not necessary.
  • Loading branch information
TomAugspurger committed Apr 29, 2018
1 parent 563a6ad commit f31cb6b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,20 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
else:
data = data.reindex(index, copy=copy)
data = data._data
elif isinstance(data, dict):
elif isinstance(data, dict) and (len(data) or index is None):
# Include the len(data) check here, since _init_dict contains
# a relatively expensive reindex. When called with
# Series(data=None, index=idx`, that is unnescessary. We know
# we're all NaN anyway, so we handle this in the next block.
# https://github.com/pandas-dev/pandas/pull/18496/
data, index = self._init_dict(data, index, dtype)
dtype = None
copy = False
elif isinstance(data, dict):
# Same as previous block, but special cased for data=None,
# for performance when creating empty arrays.
data = np.nan

elif isinstance(data, SingleBlockManager):
if index is None:
index = data.index
Expand Down

0 comments on commit f31cb6b

Please sign in to comment.