add_missing_indexes_to_series

trashpanda.add_missing_indexes_to_series(target_series: pandas.core.series.Series, new_indexes: pandas.core.indexes.base.Index, fill_value: Optional[Any] = None) pandas.core.series.Series

Adds different (missing) indexes to series.

Notes

If no explicit fill value is defined, trashpanda.DEFAULT_NA will be used, which is currently numpy.NaN. Be aware that integer arrays cannot contain NaN values as these are special values for float arrays only. To preserve integer arrays the fill value must be an integer.

Parameters
  • target_series (Series) – Series in which missing indexes should be added.

  • new_indexes (pandas.Index) – Indexes, which should be in the target series.

  • fill_value (Optional[Any]) – An optional fill value for the freshly added items.

Returns

Series

Examples

>>> from pandas import Series, Index, Int16Dtype
>>> from doctestprinter import print_pandas
>>> import numpy as np
>>> target = Series(
...     np.full(3, 1), index=list(iter("abc")), name="foo", dtype=Int16Dtype()
... )
>>> target
a    1
b    1
c    1
Name: foo, dtype: Int16

Because new indexes are represented as numpy.NaN the resulting datatype cannot be integer. In dependendy of the current python version either a object or float type is returned.

>>> new_indexes_to_add = Index(list(iter("ad")))
>>> float_sample = add_missing_indexes_to_series(target, new_indexes_to_add)
>>> str(float_sample.dtype) == str(target.dtype)
False
>>> print_pandas(float_sample)
   foo
a    1
b    1
c    1
d  nan
>>> obj_sample = add_missing_indexes_to_series(target, new_indexes_to_add, "X")
>>> obj_sample
a    1
b    1
c    1
d    X
Name: foo, dtype: object