rheon.polars.ConsumedSourceWarning

class rheon.polars.ConsumedSourceWarning[source]

Bases: RheonWarning, UserWarning

A 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 pyarrow RecordBatchReader or 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, calling pl.scan_arrow_c_stream(con.sql(q)) inside the loop, since the producer makes a new stream each time. Polars added pl.scan_arrow_c_stream in 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.rheon.fit_predict and lf.rheon.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 a pl.scan_parquet never issue it, and both can be run again. rheon.polars.stream.with_windows(), and its namespace form lf.rheon.with_windows, issue it the same way, for a run that gave no rows. rheon.polars.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 df from 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 = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
bank = rp.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.

  • RheonWarning, the base class every warning of this library shares.