In polars, the Series.std() method is used to compute the sample standard deviation of the values in a Series. It measures the extent to which data points differ from the mean.
In this article, I will explain the std() method of a Polars Series, including its syntax, behavior, and usage. This method returns a single value representing how much the Series values deviate from their average (mean).
Key Points –
- The
std()method computes the standard deviation of values in a Polars Series. - It measures how much the values deviate from the mean, indicating variability or spread.
- By default, it calculates the sample standard deviation using
ddof=1. - You can set
ddof=0to compute the population standard deviation instead. - Null values are ignored during the computation, ensuring accurate results with missing data.
- If the Series has only one valid (non-null) value, the result is None since deviation is undefined.
<a id="h-polars-series-std-introduction"></a>
Polars Series std() Introduction
Let’s know the syntax of the series std() method.
# Syntax of Series std()
Series.std(ddof: int = 1) → float | timedelta | None
<a id="h-parameters-of-the-polars-series-std"></a>
Parameters of the Polars series std()
Following are the parameters of series std() method.
ddof(int, optional) -- Delta Degrees of Freedom.
- Default is
1, which computes the sample standard deviation. - Set to
0for population standard deviation.
<a id="h-return-value"></a>
Return Value
This function returns a single float value representing the sample standard deviation of the Series.
<a id="h-usage-of-polars-series-std-method"></a>
Usage of Polars Series std() Method
The Series.std() method in Polars calculates the standard deviation of the values in a Series. It measures how much the values deviate from the mean, indicating the spread or variability in the data.
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.
To apply the std() method on a Polars Series of integers, you first create a Series representing a one-dimensional labeled array of whole numbers. This is a common approach in tasks involving numerical analysis, statistics, or data transformation.
# Compute standard deviation (default: sample std dev, ddof=1)
ser2 = ser.std()
print("Standard Deviation:", ser2)
Here,
- The values have a mean of 30.
std()calculates how far the values deviate from the mean.- Uses sample standard deviation by default (
ddof=1).
<a id="h-using-ddof-0-for-population-standard-deviation"></a>
Using ddof=0 for Population Standard Deviation
By default, Series.std() in Polars computes the sample standard deviation with ddof=1. To calculate the population standard deviation, you can set ddof=0.
# Population standard deviation (ddof=0)
ser2 = ser.std(ddof=0)
print("Population Std Dev:", ser2)
# Output:
# Population Std Dev: 14.142135623730951
Here,
std()computes the standard deviation.ddofstands for Delta Degrees of Freedom.ddof=1→ sample standard deviation (default in Polars).ddof=0→ population standard deviation.
<a id="h-series-containing-nulls"></a>
Series Containing Nulls
Polars handles null values efficiently; the std() method automatically ignores nulls when calculating the standard deviation. This ensures accurate results even when the Series contains missing data.
import polars as pl
# Series with null values
ser = pl.Series("values", [10, None, 30, None, 50])
# Compute standard deviation (sample and population)
sample_std = ser.std(ddof=1)
print("Sample Standard Deviation:", sample_std)
population_std = ser.std(ddof=0)
print("Population Standard Deviation:", population_std)
# Output:
# Sample Standard Deviation: 20.0
# Population Standard Deviation: 16.32993161855452
Here,
Noneis treated as a null- Only
[10, 20, 40, 50]are used in the calculation - Mean of valid values ≈ 30
- Deviation is calculated from the remaining 4 values
<a id="h-series-with-a-single-value"></a>
Series with a Single Value
When a Polars Series contains only a single valid value, the standard deviation is undefined because there’s no variability to measure.
import polars as pl
# Series with a single value
ser = pl.Series("values", [42])
# Compute std with both ddof=1 and ddof=0
sample_std = ser.std(ddof=1)
population_std = ser.std(ddof=0)
print("Sample Standard Deviation:", sample_std)
print("Population Standard Deviation:", population_std)
# Output:
# Sample Standard Deviation: None
# Population Standard Deviation: 0.0
Here,
- For population standard deviation (
ddof=0), it assumes you're computing variation for the entire group. Since there's only one value and no variation. - For sample standard deviation (
ddof=1), you’d divide byn-1=0, which is undefined. So Polars returns, None
<a id="h-conclusion"></a>
Conclusion
In conclusion, the std() method in Polars Series is a powerful tool for measuring the variability of numeric data. It efficiently handles null values, supports both sample and population standard deviation, and provides meaningful insights into data spread. Whether you're performing statistical analysis or cleaning data, Series.std() is a reliable method to understand how much your values deviate from the mean.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars Series slice() – Usage & Examples
- Polars quantile() – Usage & Examples
- Polars extend() Usage & Examples
- Polars abs() – Explained by Examples
- Polars Shape Usage & Examples
- Polars Series mean() – Usage & Examples
- Polars Series min() – Explained by Examples
- Polars interpolate() Function with Examples
- Polars rolling_mean() – Usage & Examples
- Polars Series limit() – Explained by Examples
- Polars struct.schema() – Explained by Examples
- Polars Series fill_null() Function with Examples
<a id="h-reference"></a>
