Built with Tutify.io

    AIStudyLabs

    Polars Series quantile() – Usage & Examples

    Polars Series quantile() – Usage & Examples

    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 –

    <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.

    <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,

    <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,

    <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,

    <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,

    <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,

    <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

    <a id="h-references"></a>

    References