to_flatten_inner#
- NestSeriesAccessor.to_flatten_inner(field: str) NestedSeries[source]#
Explode the nested inner field and return as a NestedSeries
Works for the case of multiple nesting only, the field must represent a nested series.
Each row of this Series is changed in the following way:
Each nested item in the given field is converted to a “flat” frame. If a nested item contains fields that are also nested, those are brought up as their own nested structures in the resulting “flat” frame.
All items of other fields are repeated as many times as that frame length.
It has the same effect as doing nested_df.drop(field, axis=1).join(nested_df[field].nest.to_flat()) for each nested element of the Series.
- Parameters:
field (str) – Inner field, must have NestedDtype.
- Returns:
This series object, but with the inner field exploded.
- Return type:
Examples
>>> from nested_pandas import NestedFrame >>> from nested_pandas.datasets import generate_data >>> nf = generate_data(5, 2, seed=1).rename(columns={"nested": "inner"}) >>> nf["b"] = "b" # Shorten width of example output
Assign a repeated ID to double-nest on
>>> nf["id"] = [0, 0, 0, 1, 1] >>> nf a b inner id 0 0.417022 b [{t: 8.38389, flux: 80.074457, flux_error: 1.0... 0 1 0.720324 b [{t: 13.70439, flux: 96.826158, flux_error: 1.... 0 2 0.000114 b [{t: 4.089045, flux: 31.342418, flux_error: 1.... 0 3 0.302333 b [{t: 17.562349, flux: 69.232262, flux_error: 1... 1 4 0.146756 b [{t: 0.547752, flux: 87.638915, flux_error: 1.... 1
>>> nf.inner.nest.to_flat() t flux flux_error band 0 8.38389 80.074457 1.0 r 0 13.40935 89.460666 1.0 g 1 13.70439 96.826158 1.0 g 1 8.346096 8.504421 1.0 g 2 4.089045 31.342418 1.0 g 2 11.173797 3.905478 1.0 g 3 17.562349 69.232262 1.0 r 3 2.807739 16.983042 1.0 r 4 0.547752 87.638915 1.0 g 4 3.96203 87.81425 1.0 r
Create a dataframe with double-nested column “outer”
>>> dnf = NestedFrame.from_flat(nf, base_columns=[], on="id", name="outer")
Flat “inner” nested column. This is like “concatenation” of the initial nf frame on duplicated id rows
>>> concated_nf_series = dnf["outer"].nest.to_flatten_inner("inner") >>> concated_nf_series id 0 [{a: 0.417022, b: 'b', t: 8.38389, flux: 80.07... 1 [{a: 0.302333, b: 'b', t: 17.562349, flux: 69.... Name: outer, dtype: nested<a: [double], b: [string], t: [double], flux: [double], flux_error: [double], band: [string]>
>>> concated_nf_series.nest.to_flat() a b t flux flux_error band id 0 0.417022 b 8.38389 80.074457 1.0 r 0 0.417022 b 13.40935 89.460666 1.0 g 0 0.720324 b 13.70439 96.826158 1.0 g 0 0.720324 b 8.346096 8.504421 1.0 g 0 0.000114 b 4.089045 31.342418 1.0 g 0 0.000114 b 11.173797 3.905478 1.0 g 1 0.302333 b 17.562349 69.232262 1.0 r 1 0.302333 b 2.807739 16.983042 1.0 r 1 0.146756 b 0.547752 87.638915 1.0 g 1 0.146756 b 3.96203 87.81425 1.0 r