polars_online.ModelBank.fit_predict_arrow¶
- ModelBank.fit_predict_arrow(df: DataFrame) list[ArrowStruct][source]¶
Learn a frame’s rows, and return each spec’s output as an Arrow array.
Use it to hand a bank’s output to a library other than Polars, or to take the output through the public Arrow PyCapsule interface, without Polars’ private series boundary (see Notes). For a frame back, use
fit_predict(), the same call with the output added to the frame for you.Warning
The Arrow output (this method,
predict_arrow()andArrowStruct) is unstable: it may change in any release without that counting as a breaking change. UnderPOLARS_ONLINE_WARN_UNSTABLE=1, each call issuesUnstableWarning.Parameters
dfThe next rows of the stream, as one chunk, a Polars
DataFrame, asfit_predict()takes it. Only the output is Arrow.
Returns
A list of
ArrowStruct, one per spec, in the order of the bank’s specs. Each holds the spec’s output column as an Arrow struct array, withfit_predict()’s values exactly, field for field and null for null, and is named after its spec. A consumer reads it through the Arrow PyCapsule interface (__arrow_c_array__): Polars’pl.Series, and pyarrow’spa.arrayandpa.table. DuckDB reads Arrow streams instead, so give itpl.Series(s). Each struct can be read once (see Notes).Raises
What
fit_predict()raises for the same frame.UnstableWarningA warning, not an error: issued by each call under
POLARS_ONLINE_WARN_UNSTABLE=1.
Examples
This code reads
dffrom Example data:spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) bank = po.ModelBank([spec]) structs = bank.fit_predict_arrow(df) # one per spec, as Arrow out = df.with_columns([pl.Series(s) for s in structs]) print(out["ridge"].struct.field("pred_y").count())
396
See also
fit_predict(), the same with the output added to the frame.predict_arrow(), which predicts and learns nothing.
Notes
A Polars
Seriescrosses from the extension to Python through Polars’ private_exportand_import, an interface with no promise of stability, which is whyModelBankneeds Polars 1.28.1 or later. The Arrow PyCapsule interface is a public standard, so a consumer of it reads the struct directly. Reading a struct hands its buffers to the consumer, so a second read raisesValueError. The struct’s Arrow field carries its spec’s name, whichpl.Seriesgives the series it builds.