rheon.polars.ModelBank.skip_learned

ModelBank.skip_learned(frame: DataFrame, *, clock_runs_on: bool = False) → DataFrame[source]
ModelBank.skip_learned(frame: LazyFrame, *, clock_runs_on: bool = False) → 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.

It finds the rows by each group’s last clock, so it is right only where, within a group, the clock never goes back. A spec with session or restart_after_step_back lets a group’s clock start again lower, and on such a stream the last clock is not a place in it: the rule would drop the next session’s unlearned rows and keep learned ones, and the bank would take them without a word. Nothing in the frame or the state tells that stream from one whose clock runs on across sessions, such as a timestamp with the day as the session, so under those settings the call refuses unless clock_runs_on=True says the clock runs on.

Parameters

frame

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

clock_runs_on

True declares that, within each group, the clock never goes back, across sessions and step backs included. Needed when a spec that reads a clock sets session or restart_after_step_back; accepted, and changing nothing, otherwise. Where the clock does start again, do not set it: filter the input before the bank on a column that orders the whole stream, such as a date or a row number, instead.

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; or a spec that reads a clock sets session or restart_after_step_back and clock_runs_on is not True.

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, or clock_runs_on is not a bool.

Examples

This code reads df from Example data:

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

bank = rp.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.