Is there a polars function I can use to get the the rolling highest like in the example below from df1 to df2?
df1=pl.DataFrame({"H":[3,2,1,4,6,5,9,8]})
df1
shape: (8, 1)
┌─────┐
│ H │
│ --- │
│ i64 │
╞═════╡
│ 3 │
│ 2 │
│ 1 │
│ 4 │
│ 6 │
│ 5 │
│ 9 │
│ 8 │
└─────┘
df2=pl.DataFrame({"H":[3,2,1,4,6,5,9,8],"rolling_highest":[3,3,3,4,6,6,9,9]})
df2
shape: (8, 2)
┌─────┬─────────────────┐
│ H ┆ rolling_highest │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════════════════╡
│ 3 ┆ 3 │
│ 2 ┆ 3 │
│ 1 ┆ 3 │
│ 4 ┆ 4 │
│ 6 ┆ 6 │
│ 5 ┆ 6 │
│ 9 ┆ 9 │
│ 8 ┆ 9 │
└─────┴─────────────────┘
I tried with group_by_dynamic or rolling but can get get only the highest value over a window . Would be easy extracting a list or an array and perform a cycle but would like to know if I can get the same result in polars. Thanks
Just use cum_max
In [3]: df1.with_columns(rolling_highest=pl.col('H').cum_max())
Out[3]:
shape: (8, 2)
┌─────┬─────────────────┐
│ H ┆ rolling_highest │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════════════════╡
│ 3 ┆ 3 │
│ 2 ┆ 3 │
│ 1 ┆ 3 │
│ 4 ┆ 4 │
│ 6 ┆ 6 │
│ 5 ┆ 6 │
│ 9 ┆ 9 │
│ 8 ┆ 9 │
└─────┴─────────────────┘