Derive the "_mgr" and "index" attributes of a new Series from a dictionary input. Parameters ---------- data : dict or dict-like Data used to populate the new Series. index : Index or None, default None Index for the new Serie
(
self, data: Mapping, index: Index | None = None, dtype: DtypeObj | None = None
)
| 519 | self._set_axis(0, index) |
| 520 | |
| 521 | def _init_dict( |
| 522 | self, data: Mapping, index: Index | None = None, dtype: DtypeObj | None = None |
| 523 | ): |
| 524 | """ |
| 525 | Derive the "_mgr" and "index" attributes of a new Series from a |
| 526 | dictionary input. |
| 527 | |
| 528 | Parameters |
| 529 | ---------- |
| 530 | data : dict or dict-like |
| 531 | Data used to populate the new Series. |
| 532 | index : Index or None, default None |
| 533 | Index for the new Series: if None, use dict keys. |
| 534 | dtype : np.dtype, ExtensionDtype, or None, default None |
| 535 | The dtype for the new Series: if None, infer from data. |
| 536 | |
| 537 | Returns |
| 538 | ------- |
| 539 | _data : BlockManager for the new Series |
| 540 | index : index for the new Series |
| 541 | """ |
| 542 | # Looking for NaN in dict doesn't work ({np.nan : 1}[float('nan')] |
| 543 | # raises KeyError), so we iterate the entire dict, and align |
| 544 | if data: |
| 545 | # GH:34717, issue was using zip to extract key and values from data. |
| 546 | # using generators in effects the performance. |
| 547 | # Below is the new way of extracting the keys and values |
| 548 | |
| 549 | keys = maybe_sequence_to_range(tuple(data.keys())) |
| 550 | values = list(data.values()) # Generating list of values- faster way |
| 551 | elif index is not None: |
| 552 | # fastpath for Series(data=None). Just use broadcasting a scalar |
| 553 | # instead of reindexing. |
| 554 | if len(index) or dtype is not None: |
| 555 | values = na_value_for_dtype(pandas_dtype(dtype), compat=False) |
| 556 | else: |
| 557 | values = [] |
| 558 | keys = index |
| 559 | else: |
| 560 | keys, values = default_index(0), [] |
| 561 | |
| 562 | # Input is now list-like, so rely on "standard" construction: |
| 563 | s = Series(values, index=keys, dtype=dtype) |
| 564 | |
| 565 | # Now we just make sure the order is respected, if any |
| 566 | if data and index is not None: |
| 567 | s = s.reindex(index) |
| 568 | return s._mgr, s.index |
| 569 | |
| 570 | # ---------------------------------------------------------------------- |
| 571 |
no test coverage detected