Built with Tutify.io

    AIStudyLabs

    Polars Series var() Usage & Examples

    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.

    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 –

    <a id="h-polars-series-var-introduction"></a>

    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
    

    <a id="h-parameters-of-the-polars-series-var"></a>

    Parameters of the Polars series var()

    It allows only one parameter.

    <a id="h-return-value"></a>

    Return Value

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

    <a id="h-usage-of-polars-series-var-method"></a>

    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.

    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,

    <a id="h-population-variance-ddof-0"></a>

    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,

    <a id="h-series-with-null-values"></a>

    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
    

    <a id="h-handling-float-values-in-polars-series-var"></a>

    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
    

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

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

    <a id="h-related-articles"></a>

    Related Articles

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

    References