Built with Tutify.io

    AIStudyLabs

    Polars Series max() Function

    Polars Series max() Function

    The max() function in a Polars Series is used to return the maximum (largest) value from the Series. Even if the Series contains just a single element, it still outputs a Series. The result is typically a float representing the maximum value found.

    In this article, I will explain the syntax of the Polars Series max() function, its parameters, and how to use it to find the maximum value in a Series object.

    Key Points –

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

    Polars Series max() Introduction

    Let’s know the syntax of the series max() function.

    
    # Syntax of max() function
    Series.max() → PythonLiteral | None[source]
    

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

    Parameters of the Polars Series max()

    The following are the parameters of the polars Series max() function.

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

    Return Value

    This function returns a single value (like int, float, str, etc.). Or None if the Series is empty or contains only nulls.

    <a id="h-usage-of-polars-series-max-function"></a>

    Usage of Polars Series max() Function

    The max() function in Polars Series is used to return the maximum (largest) value from the Series. This function gets the maximum value of the given object elements in Polars.

    A Polars Series is a one-dimensional array that holds data of the same type, like integers, floats, or strings.

    Now, let’s create a Polars series using a list of values and use the max() function.

    
    import polars as pl
    
    ser = pl.Series("numbers", [10, 25, 7, 42, 18])
    print("Original Series:\n", ser)
    

    Yields below output.

    The max() function in Polars returns the largest value in the Series. It works with integer values the same way it does with floats or other comparable types.

    
    # Get the maximum value
    print("Maximum value:", ser.max())
    
    # Find the maximum value
    result = ser.max()
    print("Maximum value:", result)
    

    Yields below output.

    <a id="h-using-max-with-a-single-element-series"></a>

    Using max() with a Single Element Series

    When a Polars Series contains just one element, the max() function returns that single value, as it serves as both the minimum and the maximum by default.

    
    import polars as pl
    
    # Create a Series with a single element
    ser = pl.Series("single_value", [99])
    print("Original Series:\n", ser)
    
    # Find the maximum value
    result = ser.max()
    print("Maximum value in the Series:", result)
    
    # Output:
    # Original Series:
    # shape: (1,)
    # Series: 'single_value' [i64]
    # [
    # 	99
    # ]
    # Maximum value in the Series: 99
    

    <a id="h-using-max-with-floating-point-numbers"></a>

    Using max() with Floating-Point Numbers

    The max() function in Polars works seamlessly with floating-point numbers, just like it does with integers. It evaluates the Series and returns the largest float value.

    
    import polars as pl
    
    ser = pl.Series("floats", [3.14, 2.71, 9.81, 1.41, 5.0])
    print("Original Series:\n", ser)
    
    # Find the maximum value
    result = ser.max()
    print("\nMaximum value:", result)
    
    # Output:
    # Original Series:
    # shape: (5,)
    # Series: 'floats' [f64]
    # [
    # 	3.14
    # 	2.71
    # 	9.81
    # 	1.41
    # 	5.0
    # ]
    
    # Maximum value: 9.81
    

    <a id="h-using-max-with-boolean-values"></a>

    Using max() with Boolean Values

    The Boolean values are treated numerically, False as 0 and True as 1. So when using max() on a Boolean Series, it returns True if there's at least one True value present.

    
    import polars as pl
    
    # Create a Series with boolean values
    ser = pl.Series("bools", [True, False, True, False])
    print("Original Series:\n", ser)
    
    # Find the maximum boolean value
    result = ser.max()
    print("Maximum value in the Series:", result)
    
    # Output:
    # Original Series:
    # shape: (4,)
    # Series: 'bools' [bool]
    # [
    #	true
    #	false
    #	true
    #	false
    # ]
    # Maximum value in the Series: True
    

    <a id="h-using-max-with-negative-numbers"></a>

    Using max() with Negative Numbers

    The max() function in Polars also works with negative numbers. It returns the highest value in the Series, which, for negative values, means the number closest to zero.

    
    import polars as pl
    
    # Create a Series with negative integers
    ser = pl.Series("negatives", [-12, -3, -45, -7, -19])
    print("Original Series:\n", ser)
    
    # Find the maximum (least negative) value
    result = ser.max()
    print("Maximum value in the Series:", result)
    
    # Output:
    # Original Series:
    # shape: (5,)
    # Series: 'negatives' [i64]
    # [
    #	-12
    #	-3
    #	-45
    #	-7
    #	-19
    # ]
    # Maximum value in the Series: -3
    

    <a id="h-using-max-with-missing-values"></a>

    Using max() with Missing Values

    When the Series contains missing values (nulls), the max() function in Polars skips over them and returns the highest non-null value. For example, even with None entries present, the maximum here is 25.

    
    import polars as pl
    
    # Create a Series with some null values
    ser = pl.Series("mixed", [10, None, 25, None, 18])
    print("Original Series:\n", ser)
    
    # Find the maximum value, skipping nulls
    result = ser.max()
    print("Maximum value in the Series:", result)
    
    # Output:
    # Original Series:
    # shape: (5,)
    # Series: 'mixed' [i64]
    # [
    #	10
    #	null
    #	25
    #	null
    #	18
    # ]
    # Maximum value in the Series: 25
    

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

    Conclusion

    In summary, the max() function in Polars is a straightforward yet effective way to find the highest value in a Series. It handles various data types, integers, floats, booleans, strings, and even nulls, with consistent and accurate results.

    Happy Learning!!

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

    Related Articles

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

    Reference