Built with Tutify.io

    AIStudyLabs

    Polars Series median() Function with Examples

    Polars Series median() Function with Examples

    In Polars, the Series.median() function computes the median (middle value) of a Series after sorting its elements. It returns a Python literal (like an int or float), or None if the Series is empty or consists only of null values.

    In this article, I will explain the median() method of the Polars Series, covering its syntax, parameters, and how it works. This method returns the median (middle value) of a numeric Series, and returns None if the Series is empty or contains only null values.

    Key Points –

    <a id="h-polars-series-median-introduction"></a>

    Polars Series median() Introduction

    Let’s know the syntax of the series median() method.

    
    # Syntax of Series median()
    Series.median() → PythonLiteral | None
    

    <a id="h-parameters-of-the-polars-series-median"></a>

    Parameters of the Polars series median()

    It allows only one parameter.

    <a id="h-return-value"></a>

    Return Value

    This function returns a Python literal (float or int) if values exist; otherwise returns None.

    <a id="h-usage-of-polars-series-median-method"></a>

    Usage of Polars Series median() Method

    The Series.median() function in Polars returns the median of the values in a Series, that is, the central value when the data is sorted in order.

    First, let’s create a Polars Series.

    
    import polars as pl
    
    # Sample Series with nulls
    ser = pl.Series("values", [10, 20, 30, 40, 50])
    print("Original Series:\n", ser)
    

    Yields below output.

    To compute the basic median of a Polars Series with an odd number of elements, you can use the median() method.

    
    # Compute the median
    ser2 = ser.median()
    print("Median of the Series:", ser2)
    

    Here,

    <a id="h-basic-median-even-count"></a>

    Basic Median (Even Count)

    You can calculate the median of a Polars Series with an even number of elements using the median() method.

    
    import polars as pl
    
    # Series with an even count of elements
    ser = pl.Series("values", [10, 20, 30, 40])
    
    # Compute the median
    ser2 = ser.median()
    print("Median:", ser2)
    
    # Output:
    # Median: 25.0
    

    Here,

    <a id="h-median-with-unsorted-values"></a>

    Median with Unsorted Values

    Let’s explore how Polar's handles unsorted values when computing the median using median(). Polars automatically sorts the data internally, so there’s no need to sort the values manually before calling the function.

    
    import polars as pl
    
    # Unsorted Series
    ser = pl.Series("values", [40, 10, 30, 20])
    
    # Compute the median
    ser2 = ser.median()
    print("Median:", ser2)
    
    # Output:
    # Median: 25.0
    

    Here,

    <a id="h-median-with-null-values"></a>

    Median with Null Values

    The median() method in Polars automatically excludes null (None) values when calculating the median. Let’s see how it works with a Series that contains nulls.

    
    import polars as pl
    
    # Series with null values
    ser = pl.Series("values", [10, None, 30, None, 50])
    
    # Compute the median (nulls ignored)
    ser2 = ser.median()
    print("Median (ignoring nulls):", ser2)
    
    # Output:
    # Median (ignoring nulls): 30.0
    

    Here,

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

    Conclusion

    In conclusion, the Series.median() function in Polars is a straightforward and efficient way to compute the median of numerical data. It handles unsorted values internally, automatically ignores nulls, and returns a clear result, even when working with empty or partially missing data.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

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

    References