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 –
- The
product()method computes the multiplicative product of all non-null values in a Series. - The method returns a single scalar value: either
int,float, orNone. - Ignores
nullvalues by default while computing the product. - Returns
Noneif the Series is empty or contains only nulls. - Zero values are included in the multiplication, and the result will be zero if any element is zero.
- Handles negative numbers correctly; the sign of the result reflects the number of negative terms.
<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,
- The product is computed as:
2 × 4 × 6 × 8 = 384 product()returns a single scalar value, the result of multiplying all the non-null values in the Series.
<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,
- Polars ignores nulls by default when computing the product.
- The calculation is:
2 × 4 × 6 = 48 - This behavior is similar to
sum(), nulls are skipped unless all values are null.
<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,
- The product is:
2 × 0 × 6 × 8 = 0 - Polars correctly returns
0as the product, even if other values are non-zero. - This is standard behavior across numerical libraries.
<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,
- The product is calculated as:
2 × (-3) × 4 = -24 - Polars correctly handles negative values and preserves the sign.
- If you have an odd number of negative values, the result will be negative; if it's even, the result will be positive.
<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
- Polars Series var() Usage & Examples
- Polars Series median() Function with Examples
- Polars Series std() – Explained by Examples
- Polars Series slice() – Usage & Examples
- Polars series quantile() – Usage & Examples
- Polars abs() – Explained by Examples
- Polars Series mean() – Usage & Examples
- Polars Series min() – Explained by Examples
- Polars Series interpolate() Function
- Polars rolling_mean() – Usage & Examples
- Polars Series limit() – Explained by Examples
- Polars Series fill_null() Function with Examples
<a id="h-references"></a>
