polars.Expr.approx_quantile#
- Expr.approx_quantile(
- quantile: float | list_[float] | Expr,
- *,
- method: ApproxQuantileMethod = 'auto',
- error: float = 0.001,
- error_tightness: ApproxQuantileErrorBound = 'empirical',
Compute approximate quantile(s) of an expression.
- Parameters:
- quantile
A single quantile, a list of quantiles, or an expression that resolves to a list of quantiles. The dtype of the expression must be a floating point value.
- method
Specifies which approximate-quantile algorithm is to be used. When set to ‘auto’, polars will use KLL if the quantiles are all in
[0.05, 0.95]or one of the REQ variants if any of the quantiles falls outside of the middle range.When set to
'kll', Polars will use the KLL method. This is generally the most efficient algorithm. In this case, theerrorwill specify the absolute maximum error of the rank of the quantile value that is returned. This will break down at the edges of the domain (e.g., when the quantile is 95% or greater).In the cases that you need to retain the accuracy at the edges of the domain, use
'req_lo'(for quantiles close to0),'req_hi'(for quantiles close to1), or'req_both'which computes a REQ sketch for both variants.If the method is
'auto', and thequantileis a non-literal expression, Polars will select'req_both'.- error
The allowed rank error as a factor of the number of rows in the expression. For example: if
error=0.01, and the approximate quantile is computed over 1000 rows, the rank of the returned quantile value is (with probability >99.7%) guaranteed to be at most 10 rows apart from the actual quantile.- error_tightness
The accuracy of the approximate-quantile algorithms is calibrated on shuffled inputs. However, the error bound is not mathematically sound for all possible inputs (e.g., if any of them has an adversarially “bad” order). Set this value to
'formal'to use a (looser) mathematically-sound error bound, in return for slower performance.
Notes
If the Expr has fewer than 100 million items, it is generally more efficient to use the regular
quantile()function instead.NaN values are regarded as larger than any finite number (and equal to one another). As a result,
NaNvalues are treated as the largest values when computing quantiles, which can lead to surprising results.For example, the median of
[1.0, 2.0, NaN, NaN, NaN, 6.0, 7.0]is7.0, not4.0. To excludeNaNvalues from the calculation, useExpr.drop_nans().
Examples
>>> lf = pl.select(a=pl.arange(10_000)).lazy()
>>> # Get the approximate median >>> lf.select(pl.col("a").approx_quantile(0.5)).collect() shape: (1, 1) ┌──────┐ │ a │ │ --- │ │ i64 │ ╞══════╡ │ 5000 │ └──────┘
>>> # Allow for a large error (10% of the rank) >>> lf.select( ... pl.col("a").approx_quantile(0.5, error=0.1) ... ).collect() shape: (1, 1) ┌──────┐ │ a │ │ --- │ │ i64 │ ╞══════╡ │ 4997 │ └──────┘
>>> # Explicitly use an algorithm that is accurate at the high tail >>> lf.select( ... pl.col("a").approx_quantile(0.999, method="req_hi", error=0.1) ... ).collect() shape: (1, 1) ┌──────┐ │ a │ │ --- │ │ i64 │ ╞══════╡ │ 9989 │ └──────┘