polars.Series.rolling_rank#

Series.rolling_rank(
window_size: int,
method: RankMethod = 'average',
*,
seed: int | None = None,
min_samples: int | None = None,
center: bool = False,
) Series[source]#

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_size will traverse the array. The values that fill this window will be ranked according to the method parameter. 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 to window_size.

center

Set the labels at the center of the window.

Returns:
Series

A Series of data Float64 if method is "average" or, the index size (see get_index_type()) otherwise.

Examples

>>> pl.Series([1, 4, 4, 1, 9]).rolling_rank(3, method="average")
shape: (5,)
Series: '' [f64]
[
    null
    null
    2.5
    1.0
    3.0
]