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 –
- Calculates the sum of all numeric values in a Polars Series.
- Returns a scalar value representing the total sum.
- Ignores null (missing) values by default during summation.
- Supports integer, float, and other numeric data types.
- Does not modify the original Series; returns a new scalar result.
- Supports summing Series containing negative and positive numbers.
<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.
- The return type of
sum()will be either anintor afloat, depending on the data type of the Series. - It could also return
Noneif the Series has no non-null values.
<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,
- The
sum()method adds up all the integers in the Series[1, 2, 3, 4]and returns10.
<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,
- The
sum()method adds the floats:1.5 + 2.5 + 3.0 = 7.0, and returns the result as afloat.
<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,
- Null (
None) values are skipped. - Sum is calculated as
10 + 30 + 5 = 45
<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,
- The
sum()method adds all numbers including negatives:-5 + 10 + (-3) = 2. - Negative values reduce the total sum accordingly.
<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,
- Null (
None) values are ignored. - Sum is calculated as:
10 + (-5) + 7 + (-2) = 10. - The method handles all three types seamlessly in one go.
<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
- Polars Series Unique Values
- Polars Replace String in Multiple Columns
- Polars Series sort() Usage & Examples
- Polars Series describe() – Usage & Examples
- Polars Series shift() Function with Examples
- Polars Series min() – Explained by Examples
- Polars Series sample() Function with Examples
- Polars Series list.join() Function with Examples
- Polars Series explode() – Explained by Examples
- Polars Series struct.schema() – Explained by Examples
<a id="h-references"></a>
