Skip to content

Commit f31cb6b

Browse files
committed
PERF: Fixed regression in Series(index=idx) constructor
From #18496 Special cases empty series construction, since the reindex is not necessary.
1 parent 563a6ad commit f31cb6b

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pandas/core/series.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,20 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
207207
else:
208208
data = data.reindex(index, copy=copy)
209209
data = data._data
210-
elif isinstance(data, dict):
210+
elif isinstance(data, dict) and (len(data) or index is None):
211+
# Include the len(data) check here, since _init_dict contains
212+
# a relatively expensive reindex. When called with
213+
# Series(data=None, index=idx`, that is unnescessary. We know
214+
# we're all NaN anyway, so we handle this in the next block.
215+
# https://github.com/pandas-dev/pandas/pull/18496/
211216
data, index = self._init_dict(data, index, dtype)
212217
dtype = None
213218
copy = False
219+
elif isinstance(data, dict):
220+
# Same as previous block, but special cased for data=None,
221+
# for performance when creating empty arrays.
222+
data = np.nan
223+
214224
elif isinstance(data, SingleBlockManager):
215225
if index is None:
216226
index = data.index

0 commit comments

Comments
 (0)