polars_online

Streaming and online regression models for Polars.

One Rust core, run three ways, with the same numbers from each:

  1. inside a Polars query: lf.online.fit_predict(specs) is a query that feeds its rows through a bank when it runs, and df.online.fit_predict(specs) fits a frame already in memory;

  2. ModelBank, a model bank fed one chunk at a time, whose memory grows with the models’ state and never with the number of rows that have passed. Handed a query, it reads the query a chunk at a time (ModelBank.fit_predict_batches(), ModelBank.fit());

  3. the polars-online command line: Parquet, Arrow IPC, CSV or NDJSON in and out, with the state saved and resumed between runs (docs/RUNNER.md).

A spec names a model and the columns it reads (polars_online.spec). Every way in takes a list of specs and writes one output column per spec. A target is a column, a column under a name of its own (target()), or a formula target, an expression over the window operators that looks ahead. To fit a target computed from its own row’s columns, such as a price against the mid, make the column with Polars’ with_columns in the query before the bank.

The modules around the bank:

module

what it does

polars_online.eval

scores a bank’s output

polars_online.gram

solves and diagnoses the Grams a bank exports

polars_online.stream

transforms a query: targets whose values arrive late, series that tick at their own times, and exponentially weighted means, sums, rates, variances and quantiles looking back or ahead

polars_online.ops

the window operators that polars_online.stream.with_windows() computes, and that a formula target is built from

polars_online.corr

repairs and reads correlation matrices

polars_online.sim

simulates streams whose truth is known

polars_online.gbdt

builds XGBoost’s and LightGBM’s training inputs from files or a query

Errors follow one contract throughout, and each docstring says which part of it applies:

what went wrong

the exception

a file that cannot be read or written

the OSError subclass for what went wrong (FileNotFoundError, PermissionError, …), with the path in the message

a value that is refused: a spec parameter, a config key, what a column holds

ValueError, naming the spec and the parameter or column

a column a spec reads that the frame does not have

ColumnNotFoundError, naming the spec, the column’s role and the column: Polars’ ColumnNotFoundError and a ValueError both, so either except catches it

a spec’s output column that a scoring function’s frame does not have

OutputColumnNotFoundError, a ColumnNotFoundError

a wrong type

TypeError; where the same refusal once raised ValueError, WrongTypeError

a value out of range, or a combination of settings refused, that once raised TypeError

RefusedValueError

a spec name or position a bank does not have

KeyError or IndexError

a bank fed from two threads at once

RuntimeError

Classes with two parents. A refusal that once raised a class the table above no longer gives it raises a class of this library’s own that inherits from both: the class the table gives, and the class it raised before. WrongTypeError is a TypeError and a ValueError, RefusedValueError a ValueError and a TypeError, and OutputColumnNotFoundError a ColumnNotFoundError and a KeyError. So a handler written for either class keeps catching the refusal, and new code can catch the class that names the problem. Changing the class outright would let such a refusal slip past an except that catches it today. The older parent is dropped at the next major version: catch TypeError, ValueError and ColumnNotFoundError respectively.

Inside a running Polars query the same exception arrives wrapped in Polars’ ComputeError under py-polars 1.x, carrying its message, and as itself under 2.0: how a query wraps an error is Polars’ to change. A run the bank refuses, or one abandoned before its last row, never replaces a state file. A refused chunk leaves a bank as it was, apart from the two cases ModelBank.fit_predict() names.

See also

The model bank

Run a model bank over a query, a frame or chunks you feed it, and turn the output column it adds into columns, or hand it over as Arrow. Give a target’s output fields a name of your own, or write a target as a formula that looks ahead. Read the compiled extension’s version, the version of the state files it writes, and the number of threads a bank runs on.

polars_online.ModelBank

A set of models fitted together over the same rows, fed one chunk at a time.

polars_online.fit_predict

Add each spec’s output column to a query’s or a frame’s rows, learning them.

polars_online.predict

Predict a query’s or a frame’s rows from a bank as it stands, learning nothing.

polars_online.unnest

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

polars_online.target

Name a target column’s output fields after a name of your own.

polars_online.Target

A target column under a name of its own, written as a table.

polars_online.FormulaTarget

A formula target, written as a table.

polars_online.ArrowStruct

One spec’s output column, as an Arrow struct array a consumer reads once.

polars_online.thread_pool_size

The number of threads a bank given this threads argument runs on.

polars_online.native_version

The version of the compiled extension, which equals polars_online.__version__ in a consistent install.

polars_online.schema_version

The version of the state file’s layout that this build writes.

Errors

This library’s own errors, each also a subclass of the built-in or Polars class a handler may already catch.

polars_online.ColumnNotFoundError

The error raised when a column a call reads is not in its input.

polars_online.OutputColumnNotFoundError

The error raised when a spec’s output column is not in the frame a scoring function reads.

polars_online.WrongTypeError

The error raised for a value of the wrong type where ValueError was raised before.

polars_online.RefusedValueError

The error raised for a refused value where TypeError was raised before.

Warnings

PolarsOnlineWarning, and the warnings the library issues, each a subclass of it.

polars_online.PolarsOnlineWarning

The base class of every warning this library issues.

polars_online.ReadinessWarning

A warning that a model is not ready, or may never become ready.

polars_online.OrderNotGuaranteedWarning

A warning that Polars may change the row order of a query a bank or a window run reads.

polars_online.ConsumedSourceWarning

A warning that a query whose source is written in Python may give its rows only once.

polars_online.PolarsOnlineDeprecationWarning

A warning that a renamed name was used by its old spelling.

polars_online.UnstableWarning

A warning that a part of the API not yet promised to stay the same was used.