polars_online.ConsumedSourceWarning¶
- class polars_online.ConsumedSourceWarning[source]¶
Bases:
PolarsOnlineWarning,UserWarningA warning that a query whose source is written in Python may give its rows only once.
An Arrow C stream is consumed once: the Arrow specification says a stream’s capsule “can only be consumed once”. A query built by
pl.scan_arrow_c_stream(...)over a DuckDB relation, a pyarrowRecordBatchReaderor anything else with__arrow_c_stream__gives its rows the first time it runs, and then gives none, with no error from Polars. A bank fed the same query twice learns from every row and then from none, and the second run leaves a state that looks finished and is empty. Build the query again for each run, callingpl.scan_arrow_c_stream(con.sql(q))inside the loop, since the producer makes a new stream each time. Polars addedpl.scan_arrow_c_streamin py-polars 1.43.0, so the example needs that version or later.It is a warning, not an error, because a source may have no rows: an empty query is not a mistake. The calls that read a query (
ModelBank.fit(),ModelBank.fit_predict_batches(),lf.online.fit_predictandlf.online.predict) issue it only for a query whose source is written in Python and which gave no rows, the shape a consumed stream has. A frame’s.lazy()and apl.scan_parquetnever issue it, and both can be run again.polars_online.stream.with_windows(), and its namespace formlf.online.with_windows, issue it the same way, for a run that gave no rows.polars_online.stream.embargo()issues it earlier, while it builds its query, whenever its input’s source is written in Python: the query it returns is Polars’ own, reads its input twice, and runs after the function returns.Examples
This code reads
dffrom Example data:import warnings class Once: # a producer of one Arrow stream, as a database cursor's result is def __init__(self, frame): self.stream = frame.__arrow_c_stream__() def __arrow_c_stream__(self, requested_schema=None): return self.stream rows = pl.scan_arrow_c_stream(Once(df)) spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) bank = po.ModelBank([spec]) with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always") bank.fit(rows) # learns all 400 rows bank.fit(rows) # the stream is spent: no rows print(bank.rows_fed, [type(w.message).__name__ for w in caught])
400 ['ConsumedSourceWarning']
See also
ModelBank.fit(), which reads a query a chunk at a time.PolarsOnlineWarning, the base class every warning of this library shares.