polars_online.ops.increment

polars_online.ops.increment(input: str | Expr) → Expr[source]

The step of a column from its last value within the group and session.

Use it to turn a running total, such as a cumulative volume, into the amounts each row added, before ewm_sum() or ewm_rate(). For a difference that is null on the row after a null too, use Polars’ diff().

Parameters

input

The running value: a column name, or an element-wise expression of the row. It may hold no window operator.

Returns

A pl.Expr of x_i - x_{i-1}, x_{i-1} the input’s last value, that polars_online.stream.with_windows() computes as a column, or that a window operator takes as its input. It is a float, in seconds on a temporal column. It is null on the first row of each group (of the stream, without group) and of each session, on a row whose input is null, and on a row whose clock steps back by more than restart_after_step_back, which starts the stream over, in the row’s group or on the stream across groups. A step back on the stream across groups starts every group over, so the next row of every group is null too.

Raises

TypeError

input is neither a column name nor a pl.Expr.

ValueError

input holds a window operator, a part that is not element-wise, or a column whose name starts with @po:, the prefix the operators’ own columns carry.

Examples

rows = pl.DataFrame({"t": [0.0, 1.0, 2.0, 3.0], "x": [1.0, None, 3.0, 4.0]})
out = po.stream.with_windows(rows, d=po.increment("x"), clock="t", gap_cap=10.0)
print(out["d"].to_list())   # the null row is skipped: 3 steps from 1, the last value
[None, None, 2.0, 1.0]

See also

Notes

A null input makes the row’s increment null and is skipped, as the operators hold a value from the last valued row. So [1, null, 3] gives [null, null, 2], where diff() gives three nulls. On a time of day the step is in its seconds since midnight, so a step across midnight is negative, as Polars’ diff on a Time gives.

On an integer column, Int8 to Int128 or UInt8 to UInt64, the step is taken in integers, then made a float, rounded once. So a running count past 2**53 keeps its steps of 1.

Give a sum or a rate the increments of a running total: the increments are the trades themselves. The difference of two decayed sums of the total loses precision instead. A window’s notional of 1e6, read as the difference of two such sums near 1e10, loses about four of a double’s sixteen digits.