In Polars, the Series.quantile() method is used to compute the value below which a given percentage (quantile) of the data falls. This helps you understand the data’s distribution by identifying threshold values at defined percentiles. A quantile marks the point in the data where a certain proportion of observations lie beneath it.
In this article, I will explain the Polars Series quantile() method, covering its syntax, parameters, and how it works. The method returns a float or integer representing the quantile value, or None if the Series is empty or contains only null values.
Key Points –
- The
quantile()method computes the value at a specified quantile of a Polars Series. - It accepts a quantile parameter, which must be a float between.
- The
quantileparameter accepts a float between 0 and 1 representing the desired percentile. - Supports different interpolation methods to estimate quantile values when the exact position is between data points.
- Default interpolation method is
'nearest', which selects the closest data point. - Other interpolation options include
'linear','lower','higher', and'midpoint'. - Interpolation methods determine how to calculate the quantile when it falls between two data points.
<a id="h-syntax-of-polars-series-quantile-method"></a>
Syntax of Polars Series quantile() Method
Let’s know the syntax of the series quantile() method.
# Syntax of quantile()
Series.quantile(
quantile: float,
interpolation: QuantileMethod = 'nearest',
) → float | None
<a id="h-parameters-of-the-polars-series-quantile"></a>
Parameters of the Polars series quantile()
Following are the parameters of the quantile() method.
quantile- A float between0.0and1.0indicating the desired quantile (e.g.,0.5for median).interpolation(optional) - Method to use when the quantile lies between two data points. Options:'nearest','linear','lower','higher','midpoint'. Default is'nearest'.
<a id="h-return-value"></a>
Return Value
This function returns a float value representing the quantile, or None if the Series is empty.
<a id="h-usage-of-polars-series-quantile-method"></a>
Usage of Polars Series quantile() Method
The quantile() method calculates the value in a data series below which a given percentage of the data falls. In other words, it finds the data point at a specific percentile.
First, let’s create a Polars Series.
import polars as pl
ser = pl.Series("values", [10, 20, 30, 40, 50])
print("Original Series:\n", ser)
Yields below output.
You can use the Polars quantile() method to get the median, which corresponds to the 0.5 quantile. Alternatively, to calculate the median (also called the 0.5 quantile) of a Polars Series, you can use the median() method.
# Compute the median
ser2 = ser.median()
print("Median (0.5 quantile):", ser2)
Here,
median()returns the 50th percentile of the series.- The result is a float even if the values are integers.
<a id="h-first-quartile-25th-percentile"></a>
First Quartile (25th Percentile)
To get the first quartile (25th percentile) with Polars, use the Series.quantile() method and set the quantile parameter to 0.25.
# Get the first quartile (25th percentile)
ser2 = ser.quantile(0.25)
print("First Quartile (0.25 quantile):", ser2)
# Output:
# First Quartile (0.25 quantile): 20.0
Here,
quantile(0.25)finds the value below which 25% of the data fall.- By default, the interpolation method is
'nearest', but you can specify others like'linear','lower','higher', or'midpoint'if needed.
<a id="h-third-quartile-75th-percentile"></a>
Third Quartile (75th Percentile)
To calculate the third quartile (75th percentile) in Polars, use the quantile() method with quantile=0.75.
# Calculate the 75th percentile (third quartile)
ser2 = ser.quantile(0.75)
print("\nThird Quartile (75th percentile):", ser2)
# Output:
# Third Quartile (75th percentile): 40.0
Here,
quantile(0.75)returns the value below which 75% of the data fall.- Useful for understanding data distribution, especially in box plots and statistical summaries.
<a id="h-quantile-with-linear-interpolation"></a>
Quantile with Linear Interpolation
By default, Polars uses the 'nearest' interpolation method when calculating quantiles. But you can specify linear interpolation to get a more precise estimate between data points.
# Calculate the 0.3 quantile with linear interpolation
ser2 = ser.quantile(0.3, interpolation='linear')
print("0.3 Quantile with linear interpolation:", ser2)
# Output:
# 0.3 Quantile with linear interpolation: 22.0
Here,
- Without interpolation, Polars would pick the nearest value.
- With
interpolation='linear', Polars calculates the weighted average between values around the quantile position. - For 0.3 quantile here, it’s between 20 and 30, so it returns 18.0 (closer to 20).
<a id="h-quantile-with-midpoint-interpolation"></a>
Quantile with Midpoint Interpolation
To apply the Polars quantile() method with midpoint interpolation, use it when the target quantile lies between two data points. Midpoint interpolation then calculates the quantile value as the average of these two neighboring points.
# Calculate the 0.3 quantile with midpoint interpolation
ser2 = ser.quantile(0.3, interpolation='midpoint')
print("0.3 Quantile with midpoint interpolation:", ser2)
# Output:
# 0.3 Quantile with midpoint interpolation: 25.0
Here,
- When the quantile falls between two values, midpoint interpolation returns the average (midpoint) of those two values.
- For the 0.3 quantile, which lies between 20 and 30, the midpoint is
(20 + 30) / 2 = 25.
<a id="h-conclusion"></a>
Conclusion
In conclusion, Polars' quantile() method is a powerful tool for finding specific percentiles in your data. With options like nearest, linear, and midpoint interpolation, it provides precise and customizable ways to calculate quantile values that best fit your analysis needs.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars Series extend() Usage & Examples
- Polars Series abs() – Explained by Examples
- Polars Series Shape Usage & Examples
- Polars Series sum() Method with Examples
- Polars Series describe() – Usage & Examples
- Polars Series min() – Explained by Examples
- Polars Series clear() – Explained by Examples
- Polars Series sample() Function with Examples
- Polars Series list.join() Function with Examples
- Polars Series explode() – Explained by Examples
- Polars Series rolling_mean() – Usage & Examples
- Polars Series struct.schema() – Explained by Examples
<a id="h-references"></a>
