polars_online.ModelBank.skip_learned

ModelBank.skip_learned(frame: DataFrame) → DataFrame[source]
ModelBank.skip_learned(frame: LazyFrame) → LazyFrame

The rows of a frame or query that the bank has not learned.

Use it to resume a saved bank on input that overlaps what the bank learned before it was saved, such as a rerun of the day or a file that overlaps the last one. A loaded bank goes on from the next row, so the overlap steps each group’s clock back, which fit_predict() refuses unless restart_after_step_back reads it as a new start. Without a clock in any spec, pass the bank only the rows after the last one it learned.

Parameters

frame

The input, a DataFrame or a LazyFrame, holding each spec’s clock column and group column.

Returns

frame’s rows that the bank has not learned, of the same type, in the same order: a LazyFrame stays lazy. A row is kept when, in every spec that reads a clock, its clock is after its group’s last clock, its group is one that spec has not seen, or its clock is null. A null clock is left for fit_predict() to judge. A row at its group’s last clock counts as learned.

Raises

ValueError

No spec reads a clock, so there is no position on a clock to resume from.

WrongTypeError

A clock column is not a number, a Date, a Datetime or a Duration, or a clock is temporal in the frame and was numeric in the bank, or the other way round. It is both a TypeError and a ValueError.

ColumnNotFoundError

A spec’s clock or group column is not in the frame. It is both a ValueError and Polars’ ColumnNotFoundError.

TypeError

frame is neither a DataFrame nor a LazyFrame, such as a dict.

Examples

This code reads df from Example data:

spec = po.spec.ewridge(
    "m", targets=["y"], features=["x0"], clock="t", half_life=50.0, gap_cap=10.0
)
saved = po.ModelBank([spec])
saved.fit_predict(df.head(300))   # the stream as far as the save
saved.save("bank.state")

bank = po.ModelBank.load("bank.state")
rerun = df                        # the whole day again: its first 300 rows overlap
out = bank.fit_predict(bank.skip_learned(rerun))
print(f"{out.height} rows learned, from t = {out['t'][0]}")
100 rows learned, from t = 300.0

See also

Notes

The comparison is exact. A temporal clock is compared in the integer nanoseconds the bank keeps, whatever its unit or time zone, and an integer clock in integers. Since a row at its group’s last clock counts as learned, a stream that repeats a clock value and was saved between two rows with that value loses the later ones.