Built with Tutify.io

    AIStudyLabs

    Polars Series std() – Explained by Examples

    Polars Series std() – Explained by Examples

    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 –

    <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.

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

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

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

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

    <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

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

    Reference