rheon.polars.OrderNotGuaranteedWarning¶
- class rheon.polars.OrderNotGuaranteedWarning[source]¶
Bases:
RheonWarning,UserWarningA 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.rheon.fit_predict,lf.rheon.predict, andrheon.polars.stream.with_windows()with its namespace formlf.rheon.with_windowshave 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 withwarnings.simplefilter("ignore", rheon.polars.OrderNotGuaranteedWarning).The warning names each such step and its fix:
the step
its fix
a
joinmaintain_order="left"a
group_bymaintain_order=Truea
sortby several keysmaintain_order=True: without it, rows with equal keys come out in no particular ordera
uniquea
sortafter it, since Polars does not honor itsmaintain_orderwhen it runs a query a chunk at a timeany 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:ewridgeorrlsatlam=1.0orhalf_life=inf. Each such spec must also have none of the settings that make a fit depend on the row order, such as awindow_size, a session, a weight column, an embargo, a drift reset or a diagnostic (the full list is inModelBank.fit()’s Notes). Those sums commute, so the state after that fit is the same whatever the order, to rounding, andfitkeeps only the state. Every other call and spec gets the warning:fit_predict_batchesandlf.rheon.fit_predictover 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,quantileand a reweightinghuberall 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.parquetfrom Example data:import warnings spec = rp.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.rheon.fit_predict([spec]) print([type(w.message).__name__ for w in caught])
['OrderNotGuaranteedWarning']
See also
Put rows in clock order, how to give a bank each group’s rows in order, and how to read this warning.
RheonWarning, the base class every warning of this library shares.
Notes
Measured on 200,000 rows,
collect()kept the input order andcollect_batches()did not. Over 200 rows in two orders, the state an order-freefitleaves differed by 6.7e-16 forewridgeand 1.4e-15 forrls, and the predictions offit_predict_batchesby up to 0.087 forewridgeand 0.65 forrls.The check is best effort. The query’s
explaintext is read first. Only when it names a join, an aggregation, auniqueor a sort is the query read throughLazyFrame.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.