LazyFrame.online.unnest

LazyFrameOnlineNamespace.unnest(specs: Iterable[dict[str, Any]] | ModelBank | str | PathLike[str], *, separator: str | None = None) → LazyFrame[source]

Turn each spec’s output column in a query into one column per field.

Use it on a query whose output a bank wrote, from fit_predict() or from a parquet file the command line wrote, to read its fields as plain columns or to write them to a file. For a frame in memory, use df.online.unnest. To read one field without unnesting, use pl.col("ridge").struct.field("pred_y"), with the spec’s name and the field’s.

Parameters

specs

The specs whose output columns to unnest: the spec dicts, a ModelBank (its specs), or the path of a saved state, which carries them. Each output column takes the place of its spec’s column. The rest of the query, and the output column of a spec not named, are left as they are.

separator

A text to name each column after its spec too, as {spec}{separator}{name}: separator="." gives ridge.pred_y and ridge.coef_y_x1, as Polars 2.0’s LazyFrame.unnest(separator=) names a struct’s fields. Give it whenever two specs write a field of the same name, as almost any two do: most models write weight_sum, settled_frac and withheld_reason. None, the default, names each column by its field alone. The names are made here rather than by Polars’ own separator, so it works on every Polars this library supports.

Returns

A query with each output column replaced by its fields as columns:

  • each scalar field under its own name, such as pred_y or weight_sum@h500;

  • each coef list as one column per coefficient, named coef_{target}_{term}{combo}{instance}, such as coef_y_intercept or coef_y_x1__r0.5@h500, as polars_online.spec.coef_fields() lists them;

  • support_coef, the share of each coefficient the data determined, the same way, as support_coef_{target}_{term}...;

  • se_coef as se_coef_{target}_{term}..., and the robust se_coef_hc0 and se_coef_hac as se_coef_hc0_{target}_{term}....

Raises

While the query is built:

ValueError

When:

  • a spec’s output column is not in the query, is not a struct, or lacks a field the spec writes;

  • a spec is given twice, or is not valid;

  • specs is the path of a file that is not a state this build loads (load()).

TypeError

specs is none of the three.

FileNotFoundError

specs is a path that is not there.

Polars’ DuplicateError

Two columns would get the same name, without a separator. The message names each shared field and the specs it comes from.

Examples

This code reads lf from Example data:

spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
betas = (
    lf.online.fit_predict([spec])
    .online.unnest([spec], separator=".")
    .select("t", "^ridge.coef_.*$")
    .collect()
)
print(betas.columns)
['t', 'ridge.coef_y_intercept', 'ridge.coef_y_x0', 'ridge.coef_y_x1']

See also