polars.Expr.rolling_rank#
- Expr.rolling_rank(
- window_size: int,
- method: RankMethod = 'average',
- *,
- seed: int | None = None,
- min_samples: int | None = None,
- center: bool = False,
Compute a rolling rank.
Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
A window of length
window_sizewill traverse the array. The values that fill this window will be ranked according to themethodparameter. The resulting values will be the rank of the value that is at the end of the sliding window.- Parameters:
- window_size
Integer size of the rolling window.
- method{‘average’, ‘min’, ‘max’, ‘dense’, ‘random’}
The method used to assign ranks to tied elements. The following methods are available (default is ‘average’):
‘average’ : The average of the ranks that would have been assigned to all the tied values is assigned to each value.
‘min’ : The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. (This is also referred to as “competition” ranking.)
‘max’ : The maximum of the ranks that would have been assigned to all the tied values is assigned to each value.
‘dense’ : Like ‘min’, but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements.
‘random’ : Choose a random rank for each value in a tie.
- seed
Random seed used when
method='random'. If set to None (default), a random seed is generated for each rolling rank operation.- min_samples
The number of values in the window that should be non-null before computing a result. If set to
None(default), it will be set equal towindow_size.- center
Set the labels at the center of the window.
- Returns:
- Expr
An Expr of data
Float64ifmethodis"average"or, the index size (seeget_index_type()) otherwise.
Examples
>>> df = pl.DataFrame({"a": [1, 4, 4, 1, 9]}) >>> df.select(pl.col("a").rolling_rank(3, method="average")) shape: (5, 1) ┌──────┐ │ a │ │ --- │ │ f64 │ ╞══════╡ │ null │ │ null │ │ 2.5 │ │ 1.0 │ │ 3.0 │ └──────┘