pola-rs / polars

Dataframes powered by a multithreaded, vectorized query engine, written in Rust
https://docs.pola.rs
Other
29.7k stars 1.9k forks source link

Get the quantile of the last element on rolling_window scope #8601

Open huacheng opened 1 year ago

huacheng commented 1 year ago

Problem description

I would like using the rolling_last_quantile(window_size) instead of using rank() to implemment the quantile of the last element on rolling_window scope.

now i using the rolling_apply to implemment, which like the below: import polars as pl df = pl.DataFrame( { "A": [1.0, 2.0, 9.0, 2.5, 13.0, 11, 13.5, 4], } )

agg_func = lambda d: ((d.reverse().rank('dense') - pl.lit(1)) / (d.rank('dense').max() - pl.lit(1))) df = df.lazy().with_columns( pl.col('A').rolling_apply(agg_func, 3).alias('B'), ).collect()

print(df)

ritchie46 commented 1 year ago

Can you open a Stackoverflow question?

graceyangfan commented 1 year ago

@huacheng hope this https://github.com/pola-rs/polars/issues/4808 could help you!