Apply a rolling max based on another column.
Description
Apply a rolling max based on another column.
Usage
<Expr>$rolling_max_by(by, window_size, ..., min_periods = 1, closed = "right")
Arguments
by
|
This column must of dtype Date or Datetime .
|
window_size
|
The length of the window. Can be a fixed integer size, or a dynamic
temporal size indicated by the following string language:
|
…
|
Ignored. |
min_periods
|
The number of values in the window that should be non-null before
computing a result. If NULL , it will be set equal to window
size.
|
closed
|
Define which sides of the temporal interval are closed (inclusive). This
can be either “left” , “right” ,
“both” or “none” .
|
Details
If you want to compute multiple aggregation statistics over the same
dynamic window, consider using
$rolling()
this method can cache
the window size computation.
Value
Expr
Examples
library("polars")
df_temporal = pl$DataFrame(
date = pl$datetime_range(as.Date("2001-1-1"), as.Date("2001-1-2"), "1h")
)$with_row_index("index")
df_temporal
#> shape: (25, 2)
#> ┌───────┬─────────────────────┐
#> │ index ┆ date │
#> │ --- ┆ --- │
#> │ u32 ┆ datetime[μs] │
#> ╞═══════╪═════════════════════╡
#> │ 0 ┆ 2001-01-01 00:00:00 │
#> │ 1 ┆ 2001-01-01 01:00:00 │
#> │ 2 ┆ 2001-01-01 02:00:00 │
#> │ 3 ┆ 2001-01-01 03:00:00 │
#> │ 4 ┆ 2001-01-01 04:00:00 │
#> │ … ┆ … │
#> │ 20 ┆ 2001-01-01 20:00:00 │
#> │ 21 ┆ 2001-01-01 21:00:00 │
#> │ 22 ┆ 2001-01-01 22:00:00 │
#> │ 23 ┆ 2001-01-01 23:00:00 │
#> │ 24 ┆ 2001-01-02 00:00:00 │
#> └───────┴─────────────────────┘
df_temporal$with_columns(
rolling_row_max = pl$col("index")$rolling_max_by("date", window_size = "3h")
)
#> shape: (25, 3)
#> ┌───────┬─────────────────────┬─────────────────┐
#> │ index ┆ date ┆ rolling_row_max │
#> │ --- ┆ --- ┆ --- │
#> │ u32 ┆ datetime[μs] ┆ u32 │
#> ╞═══════╪═════════════════════╪═════════════════╡
#> │ 0 ┆ 2001-01-01 00:00:00 ┆ 0 │
#> │ 1 ┆ 2001-01-01 01:00:00 ┆ 1 │
#> │ 2 ┆ 2001-01-01 02:00:00 ┆ 2 │
#> │ 3 ┆ 2001-01-01 03:00:00 ┆ 3 │
#> │ 4 ┆ 2001-01-01 04:00:00 ┆ 4 │
#> │ … ┆ … ┆ … │
#> │ 20 ┆ 2001-01-01 20:00:00 ┆ 20 │
#> │ 21 ┆ 2001-01-01 21:00:00 ┆ 21 │
#> │ 22 ┆ 2001-01-01 22:00:00 ┆ 22 │
#> │ 23 ┆ 2001-01-01 23:00:00 ┆ 23 │
#> │ 24 ┆ 2001-01-02 00:00:00 ┆ 24 │
#> └───────┴─────────────────────┴─────────────────┘