• Post author:
  • Post category:Polars
  • Post last modified:June 12, 2025
  • Reading time:10 mins read
You are currently viewing Polars Series mean() – Usage & Examples

In Polars, the Series.mean() function is used to compute the arithmetic mean (average) of all non-null numeric values in a Series. In this article, I will explain the mean() method of the Polars Series, including its syntax, parameters, and functionality. This method returns a float when the Series contains at least one non-null numeric value, and None if the Series is empty or consists entirely of nulls.

Advertisements

Key Points –

  • The Series.mean() method in Polars computes the arithmetic mean (average) of non-null numeric values in a Series.
  • It returns a Python scalar value (typically a float) representing the mean.
  • If the Series is empty or contains only null values, the method returns None.
  • Null values (None) are automatically excluded from the mean calculation.
  • The method supports both integer and float data types in the Series.
  • Series.mean() is not applicable to non-numeric data types and will raise an error if used on such Series.

Polars Series mean() Introduction

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


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

Parameters of the Polars series mean()

It allows only one parameter.

  • None — this method takes no arguments.

Return Value

This function returns a float if the Series has at least one non-null numeric value. None if all values are null or the Series is empty.

Usage of Polars Series mean() Method

The Series.mean() function in Polars calculates the arithmetic mean (average) of the values in a Series.

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.

polars series mean

The mean() method in Polars can be used to calculate the average of all numeric values in a Series. Here’s a basic example demonstrating how to use Series.mean() to compute the mean of integer values.


# Calculate the mean
mean_value = ser.mean()
print("Mean of the Series:", mean_value)

Here,

  • Polars adds all values: 10 + 20 + 30 + 40 + 50 = 150
  • Divides by count: 150 / 5 = 30.0
  • Returns the result as a float
polars series mean

Series with None Values (Nulls)

The Series.mean() method in Polars gracefully handles Series containing None (null) values. Below is an example showing how it computes the mean while ignoring nulls.


import polars as pl

# Create a Series with some None (null) values
ser = pl.Series("values_with_nulls", [10, None, 30, None, 50])

# Calculate the mean
mean_value = ser.mean()
print("\nMean of the Series (excluding nulls):", mean_value)

# Output:
# Mean of the Series (excluding nulls): 30.0

Here,

  • Polars ignores null values when calculating the mean.
  • Valid values: 10 + 30 + 50 = 90
  • Count of non-null values: 3. Mean = 90 / 3 = 30.0

Series with All None Values

Using the Polars Series.mean() method on a Series where all values are None (null) returns None.


import polars as pl

# Create a Series where all values are None
ser = pl.Series("all_nulls", [None, None, None])

# Calculate the mean
mean_value = ser.mean()
print("\nMean of the Series:", mean_value)

# Output:
# Mean of the Series: None

Here,

  • There are no valid numeric values to compute the mean.
  • So Polars returns: None

Series with Negative and Positive Numbers

You can also use the Polars Series.mean() method with a combination of negative and positive numbers. It accurately computes the mean regardless of the sign of the values.


import polars as pl

# Create a Series with both negative and positive values
ser = pl.Series("mixed_values", [-10, -5, 0, 5, 10])

# Calculate the mean
mean_value = ser.mean()
print("\nMean of the Series:", mean_value)

# Output:
# Mean of the Series: 0.0

Here,

  • The sum is: -10 + (-5) + 0 + 5 + 10 = 0
  • The count of values is: 5
  • Therefore, the mean is: 0 ÷ 5 = 0.0

Conclusion

In conclusion, the Polars Series.mean() method is a simple yet powerful tool for calculating the average of numeric values in a Series. It handles nulls gracefully by ignoring them, returns None for empty or all-null Series, and works seamlessly with both positive and negative numbers.

Happy Learning!!

References