rheon.polars.ops.increment¶
- rheon.polars.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()orewm_rate(). For a difference that is null on the row after a null too, use Polars’diff().Parameters
inputThe running value: a column name, or an element-wise expression of the row. It may hold no window operator.
Returns
A
pl.Exprofx_i - x_{i-1},x_{i-1}the input’s last value, thatrheon.polars.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, withoutgroup) and of each session, on a row whose input is null, and on a row whose clock steps back by more thanrestart_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
TypeErrorinputis neither a column name nor apl.Expr.ValueErrorinputholds 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 = rp.stream.with_windows(rows, d=rp.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
ewm_sum()andewm_rate(), which take its steps.rheon.polars.stream.with_windows(), which computes the operators over a stream.
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], wherediff()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’diffon aTimegives.On an integer column,
Int8toInt128orUInt8toUInt64, the step is taken in integers, then made a float, rounded once. So a running count past2**53keeps 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.