add_blank_rows
- trashpanda.add_blank_rows(source: pandas.core.series.Series, indexes_to_add: Union[Iterable, pandas.core.indexes.base.Index], fill_value: Optional[Any] = None, override: bool = False) pandas.core.series.Series
- trashpanda.add_blank_rows(source: pandas.core.frame.DataFrame, indexes_to_add: Union[Iterable, pandas.core.indexes.base.Index], fill_value: Optional[Any] = None, override: bool = False) pandas.core.frame.DataFrame
Adds blank rows into a Series or DataFrame with numpy.nan by default. Double indexes are either overriden if argumend override is True or ignored.
- Parameters
source (Union[Series, DataFrame]) – Series or DataFrame in which additional ‘blank’ rows should be filled.
indexes_to_add (Union[Iterable, pandas.Index]) – The targeted indexes, which are going to be added or overriden.
fill_value (Optional[Any]) – Default numpy.nan; value which is going to be used as the added rows values.
override (bool) – States if the indexes to add are overriding the source or ignored.
- Returns
Union[Series, DataFrame]
Examples
Usage with a pandas.Series
>>> from pandas import Series, Index >>> import numpy as np >>> from doctestprinter import doctest_print >>> from trashpanda import add_blank_rows >>> sample_series = Series( ... np.arange(3.0), name="a", index=Index([0.1, 0.2, 0.3], name="x") ... ) >>> prepared_frame = add_blank_rows( ... source=sample_series, indexes_to_add=[0.15, 0.3, 0.35] ... ) >>> doctest_print(prepared_frame) x 0.10 0.0 0.15 NaN 0.20 1.0 0.30 2.0 0.35 NaN Name: a, dtype: float64 >>> doctest_print( ... prepared_frame.interpolate(method="index", limit_area="inside") ... ) x 0.10 0.0 0.15 0.5 0.20 1.0 0.30 2.0 0.35 NaN Name: a, dtype: float64 >>> doctest_print( ... add_blank_rows( ... source=sample_series, ... indexes_to_add=[0.15, 0.3, 0.35], ... override=True ... ) ... ) x 0.10 0.0 0.15 NaN 0.20 1.0 0.30 NaN 0.35 NaN Name: a, dtype: float64 >>> doctest_print( ... add_blank_rows( ... source=sample_series, indexes_to_add=[], ... ) ... ) x 0.1 0.0 0.2 1.0 0.3 2.0 Name: a, dtype: float64
Usage with a pandas.DataFrame
>>> from pandas import DataFrame, Index >>> import numpy as np >>> from doctestprinter import doctest_print >>> from trashpanda import add_blank_rows >>> sample_series = DataFrame( ... np.arange(6.0).reshape(3, 2), ... columns=["b", "a"], ... index=Index([0.1, 0.2, 0.3], name="x") ... ) >>> prepared_frame = add_blank_rows( ... source=sample_series, indexes_to_add=[0.15, 0.3, 0.35] ... ) >>> doctest_print(prepared_frame) b a x 0.10 0.0 1.0 0.15 NaN NaN 0.20 2.0 3.0 0.30 4.0 5.0 0.35 NaN NaN >>> doctest_print( ... prepared_frame.interpolate(method="index", limit_area="inside") ... ) b a x 0.10 0.0 1.0 0.15 1.0 2.0 0.20 2.0 3.0 0.30 4.0 5.0 0.35 NaN NaN >>> doctest_print( ... add_blank_rows( ... source=sample_series, ... indexes_to_add=[0.15, 0.3, 0.35], ... override=True ... ) ... ) b a x 0.10 0.0 1.0 0.15 NaN NaN 0.20 2.0 3.0 0.30 NaN NaN 0.35 NaN NaN >>> doctest_print( ... add_blank_rows( ... source=sample_series, indexes_to_add=[], ... ) ... ) b a x 0.1 0.0 1.0 0.2 2.0 3.0 0.3 4.0 5.0