polars_online.OrderNotGuaranteedWarning

class polars_online.OrderNotGuaranteedWarning[source]

Bases: PolarsOnlineWarning, UserWarning

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

An online model learns in row order, so the order a query delivers is part of the model. ModelBank.fit(), ModelBank.fit_predict_batches(), lf.online.fit_predict, lf.online.predict, and polars_online.stream.with_windows() with its namespace form lf.online.with_windows have Polars run a query a chunk at a time. Each issues this warning when the query has a step whose row order Polars may then change. Fix the step it names, or, for a query whose order is fixed by other means, filter the warning out with warnings.simplefilter("ignore", polars_online.OrderNotGuaranteedWarning).

The warning names each such step and its fix:

the step

its fix

a join

maintain_order="left"

a group_by

maintain_order=True

a sort by several keys

maintain_order=True: without it, rows with equal keys come out in no particular order

a unique

a sort after it, since Polars does not honor its maintain_order when it runs a query a chunk at a time

any of them

a sort before the bank or the window run

When Polars runs the query a chunk at a time, such a step without an order guarantee delivers a different stream than lf.collect() gives, and the order may differ between runs (see Notes). So a query checked by running it and reading its result learns something else when a bank is fed from it.

ModelBank.fit() does not issue it when every spec keeps running sums with no decay: ewridge or rls at lam=1.0 or half_life=inf. Each such spec must also have none of the settings that make a fit depend on the row order, such as a window_size, a session, a weight column, an embargo, a drift reset or a diagnostic (the full list is in ModelBank.fit()’s Notes). Those sums commute, so the state after that fit is the same whatever the order, to rounding, and fit keeps only the state. Every other call and spec gets the warning:

  • fit_predict_batches and lf.online.fit_predict over the same specs, whose predictions are out-of-sample and so move with the order even where the coefficients do not;

  • every other model, whose step at each row does not commute: sgd, pa, ftrl, quantile and a reweighting huber all differ materially with no decay at all;

  • lasso, which selects its penalty by an out-of-sample error.

So a spec with no half-life is not by itself one whose output ignores the row order. A spec setting the check does not recognize counts as unsafe, so a setting added to the library later cannot quietly become exempt.

Examples

This code reads ticks.parquet from Example data:

import warnings

spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
sides = pl.scan_parquet("ticks.parquet").select("t", "x0", "x1")
# A join with no maintain_order: Polars may deliver its rows in any order.
rows = pl.scan_parquet("ticks.parquet").select("t", "y").join(sides, on="t")
with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")
    rows.online.fit_predict([spec])
print([type(w.message).__name__ for w in caught])
['OrderNotGuaranteedWarning']

See also

Notes

Measured on 200,000 rows, collect() kept the input order and collect_batches() did not. Over 200 rows in two orders, the state an order-free fit leaves differed by 6.7e-16 for ewridge and 1.4e-15 for rls, and the predictions of fit_predict_batches by up to 0.087 for ewridge and 0.65 for rls.

The check is best effort. The query’s explain text is read first. Only when it names a join, an aggregation, a unique or a sort is the query read through LazyFrame.serialize, a format Polars has deprecated, and walked. A query Polars cannot serialize is let through without a warning. Polars 1.44.2 serializes a query that already holds a bank, its Python source included, so a join stacked after a bank is reported there. A Polars that cannot serialize it says nothing about it. The check never fails a run.