rheon.polars.ModelBank.groups

ModelBank.groups(spec: str | int | None = None) → DataFrame[source]

The groups the bank holds a model for, one row for each spec and group.

Use it to see which groups a bank has learned, such as a bank loaded from a state file, and to find the groups that have gone quiet, whose models stay in the bank until drop_groups() removes them.

Parameters

spec

A spec’s name or position, which narrows the table to that spec. None, the default, lists every spec.

Returns

A DataFrame with one row for each spec and group, in the bank’s spec order, each spec’s groups sorted by key: integer keys as numbers, text keys as text, the null group first. Its columns:

spec, group

The spec’s name, and the group key as a string: "" for a spec without a group column, and null for the rows whose key was null. A Datetime key with a time zone is written as its instant’s UTC wall time.

rows_fed

The rows the bank routed to the group, skipped rows included, as summary() counts them.

clock_last

The group’s last clock value, exactly, in the clock column’s own type: a Datetime in its unit and time zone, a Date, a Duration, an integer clock in its own width, and a Float64 for a float clock (the type emit_clocks gives scored_clock). It is null before the group’s first row, and on a spec without a clock. The column is a Float64 of nulls only when no spec read has a clock.

Raises

KeyError

spec is a name the bank does not hold. The message lists the names.

IndexError

spec is a position the bank does not hold.

ValueError

spec is None and the specs read clocks of different types, which one clock_last column cannot hold. Read such a bank one spec at a time.

Examples

This code reads df from Example data:

spec = rp.spec.ewridge(
    "ridge", targets=["y"], features=["x0"], group="stock_id",
    clock="ts", gap_cap="5m", half_life="1h",
)
bank = rp.ModelBank([spec])
bank.fit_predict(df)
print(bank.groups())
# the groups with no row in the last 2 minutes of the stream
cutoff = pl.lit(df["ts"].max()) - pl.duration(minutes=2)
quiet = bank.groups().filter(pl.col("clock_last") < cutoff)
print(bank.drop_groups(quiet["group"]))
shape: (4, 4)
┌───────┬───────┬──────────┬─────────────────────┐
│ spec  ┆ group ┆ rows_fed ┆ clock_last          │
│ ---   ┆ ---   ┆ ---      ┆ ---                 │
│ str   ┆ str   ┆ i64      ┆ datetime[μs]        │
╞═══════╪═══════╪══════════╪═════════════════════╡
│ ridge ┆ b0    ┆ 100      ┆ 2024-01-02 16:06:00 │
│ ridge ┆ b1    ┆ 100      ┆ 2024-01-02 16:07:00 │
│ ridge ┆ b2    ┆ 100      ┆ 2024-01-02 16:08:00 │
│ ridge ┆ b3    ┆ 100      ┆ 2024-01-02 16:09:00 │
└───────┴───────┴──────────┴─────────────────────┘
1

See also