|
42 | 42 | _default_index,
|
43 | 43 | _asarray_tuplesafe,
|
44 | 44 | _values_from_object,
|
45 |
| - _try_sort, |
46 | 45 | _maybe_match_name,
|
47 | 46 | SettingWithCopyError,
|
48 | 47 | _maybe_box_datetimelike,
|
@@ -198,18 +197,9 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
|
198 | 197 | data = data.reindex(index, copy=copy)
|
199 | 198 | data = data._data
|
200 | 199 | elif isinstance(data, dict):
|
201 |
| - if index is None: |
202 |
| - if isinstance(data, OrderedDict): |
203 |
| - index = Index(data) |
204 |
| - else: |
205 |
| - index = Index(_try_sort(data)) |
206 |
| - |
207 |
| - try: |
208 |
| - data = index._get_values_from_dict(data) |
209 |
| - except TypeError: |
210 |
| - data = ([data.get(i, np.nan) for i in index] |
211 |
| - if data else np.nan) |
212 |
| - |
| 200 | + data, index = self._init_dict(data, index, dtype) |
| 201 | + dtype = None |
| 202 | + copy = False |
213 | 203 | elif isinstance(data, SingleBlockManager):
|
214 | 204 | if index is None:
|
215 | 205 | index = data.index
|
@@ -257,6 +247,43 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
|
257 | 247 | self.name = name
|
258 | 248 | self._set_axis(0, index, fastpath=True)
|
259 | 249 |
|
| 250 | + def _init_dict(self, data, index=None, dtype=None): |
| 251 | + """ |
| 252 | + Derive the "_data" and "index" attributes of a new Series from a |
| 253 | + dictionary input. |
| 254 | +
|
| 255 | + Parameters |
| 256 | + ---------- |
| 257 | + data : dict or dict-like |
| 258 | + Data used to populate the new Series |
| 259 | + index : Index or index-like, default None |
| 260 | + index for the new Series: if None, use dict keys |
| 261 | + dtype : dtype, default None |
| 262 | + dtype for the new Series: if None, infer from data |
| 263 | +
|
| 264 | + Returns |
| 265 | + ------- |
| 266 | + _data : BlockManager for the new Series |
| 267 | + index : index for the new Series |
| 268 | + """ |
| 269 | + # Looking for NaN in dict doesn't work ({np.nan : 1}[float('nan')] |
| 270 | + # raises KeyError), so we iterate the entire dict, and align |
| 271 | + if data: |
| 272 | + keys, values = zip(*compat.iteritems(data)) |
| 273 | + else: |
| 274 | + keys, values = [], [] |
| 275 | + # Input is now list-like, so rely on "standard" construction: |
| 276 | + s = Series(values, index=keys, dtype=dtype) |
| 277 | + # Now we just make sure the order is respected, if any |
| 278 | + if index is not None and not index.identical(keys): |
| 279 | + s = s.reindex(index) |
| 280 | + elif not isinstance(data, OrderedDict): |
| 281 | + try: |
| 282 | + s = s.sort_index() |
| 283 | + except TypeError: |
| 284 | + pass |
| 285 | + return s._data, s.index |
| 286 | + |
260 | 287 | @classmethod
|
261 | 288 | def from_array(cls, arr, index=None, name=None, dtype=None, copy=False,
|
262 | 289 | fastpath=False):
|
|
0 commit comments