LazyFrame.rheon.unnest¶
- LazyFrameRheonNamespace.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, usedf.rheon.unnest. To read one field without unnesting, usepl.col("ridge").struct.field("pred_y"), with the spec’s name and the field’s.Parameters
specsThe 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.separatorA text to name each column after its spec too, as
{spec}{separator}{name}:separator="."givesridge.pred_yandridge.coef_y_x1, as Polars 2.0’sLazyFrame.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 writeweight_sum,settled_fracandwithheld_reason.None, the default, names each column by its field alone. The names are made here rather than by Polars’ ownseparator, 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_yorweight_sum@h500;each
coeflist as one column per coefficient, namedcoef_{target}_{term}{combo}{instance}, such ascoef_y_interceptorcoef_y_x1__r0.5@h500, asrheon.spec.coef_fields()lists them;support_coef, the share of each coefficient the data determined, the same way, assupport_coef_{target}_{term}...;se_coefasse_coef_{target}_{term}..., and the robustse_coef_hc0andse_coef_hacasse_coef_hc0_{target}_{term}....
Raises
While the query is built:
ValueErrorWhen:
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;
specsis the path of a file that is not a state this build loads (load()).
TypeErrorspecsis none of the three.FileNotFoundErrorspecsis 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
lffrom Example data:spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) betas = ( lf.rheon.fit_predict([spec]) .rheon.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
rheon.spec.coef_fields(), each coefficient’s column name and where it sits incoef.rheon.spec.output_fields(), the fields a spec writes.rheon.polars.unnest(), this method as a function a type checker can see.