Built with Tutify.io

    AIStudyLabs

    Polars Series product() Method with Examples

    Polars Series product() Method with Examples

    In Polars, the Series.product() method is used to compute the product of all values within a Series. Similar to sum(), which adds elements, this method multiplies them together and returns the result.

    In this article, I will explain the product() method of the Polars Series, covering its syntax, parameters, and how it works. This method returns a single scalar value, either an int, float, or None if the Series is empty or contains only null values.

    Key Points –

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

    Polars Series product() Introduction

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

    
    # Syntax of product()
    Series.product() → int | float
    

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

    Parameters of the Polars Series product()

    Computes the cumulative multiplication of all values in the Series.

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

    Return Value

    This function returns an int or float, depending on the data type. Ignores null values by default.

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

    Usage of Polars Series product() Method

    The product() method in Polars calculates the multiplicative result of all non-null values in a Series. It multiplies all the elements together to return their total product.

    Now, let’s create a Polars Series.

    
    import polars as pl
    
    # Sample Series with nulls
    ser = pl.Series("values", [2, 4, 6, 8])
    print("Original Series:\n", ser)
    

    Yields below output.

    To calculate the basic integer product of a Polars Series, you can use the product() method.

    
    # Calculate the product of all values
    ser2 = ser.product()
    print("Product of values:", ser2)
    

    Here,

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

    Series with Null Values

    The product() method in Polars can handle Series containing null (or None) values. It computes the product using only the non-null elements, effectively ignoring any nulls in the Series.

    
    import polars as pl
    
    # Create a Series with some null values
    ser = pl.Series("values", [2, None, 4, 6])
    
    # Compute the product, ignoring nulls
    ser2 = ser.product()
    print("Product of non-null values:", ser2)
    
    # Output:
    # Product of non-null values: 48
    

    Here,

    <a id="h-product-with-zero-in-series"></a>

    Product with Zero in Series

    If your Polars Series contains a zero, the product() method will return 0, since multiplying by zero nullifies the entire product, just like in mathematics.

    
    import polars as pl
    
    # Series with a zero
    ser_with_zero = pl.Series("values", [2, 0, 6, 8])
    
    # Compute product
    ser2 = ser_with_zero.product()
    print("Product of values:", ser2)
    
    # Output:
    # Product of values: 0
    

    Here,

    <a id="h-negative-numbers"></a>

    Negative Numbers

    When your Polars Series contains negative numbers, the product() method includes them in the multiplication, following standard arithmetic rules.

    
    import polars as pl
    
    # Series with negative numbers
    ser_negative = pl.Series("values", [2, -3, 4])
    
    # Compute product
    ser2 = ser_negative.product()
    print("Product of values:", ser2)
    
    # Output:
    # Product of values: -24
    

    Here,

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

    Conclusion

    In conclusion, the product() method in Polars provides a simple and effective way to calculate the multiplicative product of values in a Series. It gracefully handles null values, includes negative numbers as per arithmetic rules, and returns zero if any element is zero. This makes it a reliable choice for performing product-based aggregations in data analysis.

    Happy Learning!!

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

    Related Articles

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

    References