Built with Tutify.io

    AIStudyLabs

    Polars Series sum() Method with Examples

    Polars Series sum() Method with Examples

    The sum() method in a Polars Series is a built-in function that computes the total (sum) of all numeric values in the Series, excluding any nulls. It returns the overall sum as an integer or float, depending on the data type.

    In this article, I will explain the Polars Series.sum() method, covering its syntax, parameters, and usage, and show how it computes the total of all numeric values in a Series.

    Key Points –

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

    Polars Series sum() Introduction

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

    
    # Syntax of series sum()
    Series.sum() → int | float
    

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

    Parameters of the Polars Series sum()

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

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

    Return Value

    This function returns the sum of all non-null values in the Series.

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

    Usage of Polars Series sum() Function

    The Series.sum() method in Polars calculates the sum of all numeric values in a Series.

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

    
    import polars as pl
    
    ser = pl.Series("numbers", [2, 4, 6, 8])
    print("Original Series:\n", ser)
    

    Yields below output.

    To get the basic sum of the integers in your Polars Series, you can use the sum() method.

    
    # Calculate the sum of the Series
    total_sum = ser.sum()
    print("Sum of the Series:", total_sum)
    

    Here,

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

    Sum with Floating-Point Numbers

    Using the Polars Series.sum() method on a Series of floating-point numbers adds up all the non-null float values and returns their total as a float. Below is an example demonstrating how sum() handles floating-point numbers in a Polars Series.

    
    import polars as pl
    
    ser = pl.Series("weights", [1.5, 2.3, 3.2])
    total = ser.sum()
    print("Sum of the Series:", total)
    
    # Output:
    # Sum of the Series: 7.0
    

    Here,

    <a id="h-sum-ignoring-null-values"></a>

    Sum Ignoring Null Values

    By default, Polars automatically ignores null values when summing a Series. Calling sum() on a Polars Series with null (missing) values will skip those nulls and sum only the valid numeric entries.

    
    import polars as pl
    
    ser = pl.Series("numbers", [2, None, 6, 8])
    
    # Calculate sum, nulls are ignored
    total_sum = ser.sum()
    print("Sum of the Series (ignoring nulls):", total_sum)
    
    # Output:
    # Sum of the Series (ignoring nulls): 16
    

    Here,

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

    Sum with Negative Numbers

    The Polars Series.sum() method correctly handles negative numbers. When applied to a Series with negative values, it adds them together along with the positive numbers, following standard arithmetic rules.

    
    import polars as pl
    
    ser = pl.Series("negatives", [-5, 10, -3])
    
    # Series with negative and positive numbers
    total_sum = ser.sum()
    print("Sum including negative numbers:", total_sum)
    
    # Output:
    # Sum including negative numbers: 2
    

    Here,

    <a id="h-sum-of-mixed-positive-negative-and-null-values"></a>

    Sum of Mixed Positive, Negative, and Null Values

    When you call sum() on a Polars Series that includes positive numbers, negative numbers, and nulls, it correctly sums all non-null values.

    
    import polars as pl
    
    ser = pl.Series("numbers", [10, -5, None, 7, -2])
    
    # Calculate the sum (nulls are ignored)
    total_sum = ser.sum()
    print("Sum of mixed values:", total_sum)
    
    # Output:
    # Sum of mixed values: 10
    

    Here,

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

    Conclusion

    In conclusion, polars Series.sum() method is a powerful and straightforward way to calculate the total of numeric values in a Series. It efficiently handles positive and negative numbers, automatically ignores null values, and returns the sum as a scalar. Whether working with integers or floating-point numbers, sum() simplifies aggregation tasks in Polars with reliable performance.

    Happy Learning!!

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

    Related Articles

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

    References