|
41 | 41 | maybe_cast_to_datetime, maybe_castable, |
42 | 42 | construct_1d_arraylike_from_scalar, |
43 | 43 | construct_1d_object_array_from_listlike) |
44 | | -from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike |
| 44 | +from pandas.core.dtypes.missing import ( |
| 45 | + isna, |
| 46 | + notna, |
| 47 | + remove_na_arraylike, |
| 48 | + na_value_for_dtype) |
45 | 49 |
|
46 | 50 | from pandas.core.index import (Index, MultiIndex, InvalidIndexError, |
47 | 51 | Float64Index, _ensure_index) |
@@ -300,16 +304,23 @@ def _init_dict(self, data, index=None, dtype=None): |
300 | 304 | if data: |
301 | 305 | keys, values = zip(*compat.iteritems(data)) |
302 | 306 | values = list(values) |
| 307 | + elif index is not None: |
| 308 | + # fastpath for Series(data=None). Just use broadcasting a scalar |
| 309 | + # instead of reindexing. |
| 310 | + values = na_value_for_dtype(dtype) |
| 311 | + keys = index |
303 | 312 | else: |
304 | 313 | keys, values = [], [] |
305 | 314 |
|
306 | 315 | # Input is now list-like, so rely on "standard" construction: |
307 | 316 | s = Series(values, index=keys, dtype=dtype) |
308 | 317 |
|
309 | 318 | # Now we just make sure the order is respected, if any |
310 | | - if index is not None: |
| 319 | + if data and index is not None: |
311 | 320 | s = s.reindex(index, copy=False) |
312 | | - elif not PY36 and not isinstance(data, OrderedDict): |
| 321 | + elif not PY36 and not isinstance(data, OrderedDict) and data: |
| 322 | + # Need the `and data` to avoid sorting Series(None, index=[...]) |
| 323 | + # since that isn't really dict-like |
313 | 324 | try: |
314 | 325 | s = s.sort_index() |
315 | 326 | except TypeError: |
|
0 commit comments