• Post author:
  • Post category:Polars
  • Post last modified:June 20, 2025
  • Reading time:9 mins read
You are currently viewing Polars Series var() Usage & Examples

In Polars, the var() method is used to compute the variance of values within a Series. This is a statistical measure of the spread between numbers in the data. Variance is useful when you want to understand how much the values in a dataset deviate from the mean.

Advertisements

In this article, I will explain the var() method of the Polars Series, detailing its syntax, parameters, and functionality. This method calculates the sample variance of the Series values, a statistical measure of how much the values deviate from the mean.

Key Points –

  • Computes the variance of values in a Series, measuring the dispersion from the mean.
  • Series.var() calculates the sample variance of the values in a Polars Series by default.
  • The method supports both integer and floating-point (float32, float64) numeric data types.
  • By default, ddof=1 is used, which computes the sample variance (dividing by n−1).
  • You can pass ddof=0 to calculate the population variance (dividing by n).
  • Null values are automatically ignored, and variance is calculated on the non-null values.

Polars Series var() Introduction

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


# Syntax of var()
Series.var(ddof: int = 1) → float | timedelta | None

Parameters of the Polars series var()

It allows only one parameter.

  • ddof (optional, default = 1) – Stands for Delta Degrees of Freedom.
  • Controls the denominator in the variance formula.
    • ddof=1: Uses sample variance (divides by n-1)
    • ddof=0: Uses population variance (divides by n)

Return Value

This function returns the value of the Series.var() depends on the type of data in the Series.

Usage of Polars Series var() Method

The Series.var() method in Polars calculates the sample variance of the numeric values in a Series. It measures how much the values spread out or deviate from the mean.

Now, 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 var

To calculate the basic sample variance of a Polars Series, you can use the var() method. By default, it uses ddof=1, so simply calling var() on the Series will return the sample variance.


# Compute sample variance (default ddof=1)
variance = ser.var()
print("Sample Variance (ddof=1):", variance)

Here,

  • The mean of [10, 20, 30, 40, 50] is 30.
  • The squared deviations from the mean are: (10−30)² = 400, (20−30)² = 100, (30−30)² = 0, (40−30)² = 100, (50−30)² = 400. Sum = 1000
polars series var

Population Variance (ddof=0)

To compute the population variance in Polars, pass ddof=0 to the var() method. This ensures the calculation uses the total number of values as the denominator.


# Compute population variance
pop_variance = ser.var(ddof=0)
print("Population Variance (ddof=0):", pop_variance)

# Output:
# Population Variance (ddof=0): 200.0

Here,

  • Same data: [10, 20, 30, 40, 50]. Mean = 30.
  • Squared deviations = 1000 (same as in sample variance)
  • Population variance formula = 200.0

Series with Null Values

When a Polars Series contains null values, the var() method automatically excludes them and computes the variance using only the non-null entries. To compute the population variance, simply pass ddof=0 to the var() method.


import polars as pl

# Series with a null value
ser = pl.Series("values", [10, 20, None, 40, 50])

# Sample variance (ddof=1) – default behavior
sample_var = ser.var()
print("Sample Variance (ddof=1) with nulls:", sample_var)

# Population variance (ddof=0)
pop_var = ser.var(ddof=0)
print("Population Variance (ddof=0) with nulls:", pop_var)

# Output:
# Sample Variance (ddof=1) with nulls: 333.3333333333333
# Population Variance (ddof=0) with nulls: 250.0

Handling Float Values in Polars Series.var()

Let’s see how Polars Series.var() works with float values; it fully supports both float32 and float64 types and behaves similarly to how it handles integers.


import polars as pl

# Float Series
ser = pl.Series("measurements", [1.2, 3.4, 5.6, 7.8])

# Sample variance (default ddof=1)
print("Sample Variance (ddof=1):", ser.var())

# Population variance (ddof=0)
print("Population Variance (ddof=0):", ser.var(ddof=0))

# Output:
# Sample Variance (ddof=1): 8.066666666666665
# Population Variance (ddof=0): 6.049999999999999

Conclusion

In conclusion, the var() method in the Polars Series is a straightforward yet powerful tool for measuring the variability of data. It supports both integer and floating-point values, handles nulls gracefully, and allows flexibility through the ddof parameter for computing either sample or population variance.

Happy Learning!!

References