NumPy nanprod()
The numpy.nanprod()
function calculates the product of array elements while treating NaN
(Not a Number) values as 1
.
This ensures that NaN
values do not affect the final product.
Syntax
</>
Copy
numpy.nanprod(a, axis=None, dtype=None, out=None, keepdims=False, initial=<no value>, where=True)
Parameters
Parameter | Type | Description |
---|---|---|
a | array_like | Input array whose product is computed. If not already an array, it is converted. |
axis | int, tuple of int, None, optional | Specifies the axis along which to compute the product. If None , the product of all elements is computed. |
dtype | data-type, optional | Specifies the data type of the result and the accumulator used in computation. |
out | ndarray, optional | Alternative array where results are stored. It must have the same shape as the expected output. |
keepdims | bool, optional | If True , reduced dimensions are kept as size one, ensuring correct broadcasting. |
initial | scalar, optional | Starting value for the product computation. |
where | array_like of bool, optional | Determines which elements to include in the computation. |
Return Value
Returns an array containing the product of the input elements while treating NaN
values as 1
. If out
is provided, it is returned instead.
Examples
1. Computing Product While Ignoring NaN Values
In this example, we calculate the product of an array containing NaN
values.
</>
Copy
import numpy as np
# Define an array with NaN values
arr = np.array([2, np.nan, 5, 3])
# Compute the product while treating NaN as 1
result = np.nanprod(arr)
# Print the result
print("Product ignoring NaN values:", result)
Output:
Product ignoring NaN values: 30.0

2. Using the axis
Parameter in nanprod()
We calculate the product along different axes of a 2D array while ignoring NaN
values.
</>
Copy
import numpy as np
# Define a 2D array with NaN values
arr = np.array([[1, 2, np.nan],
[4, np.nan, 6]])
# Compute product along axis 0 (columns)
result_axis0 = np.nanprod(arr, axis=0)
print("Product along axis 0 (columns):", result_axis0)
# Compute product along axis 1 (rows)
result_axis1 = np.nanprod(arr, axis=1)
print("Product along axis 1 (rows):", result_axis1)
Output:
Product along axis 0 (columns): [ 4. 2. 6.]
Product along axis 1 (rows): [ 2. 24.]

3. Using keepdims=True
to Preserve Dimensions
We use keepdims=True
to maintain the dimensions of the original array.
</>
Copy
import numpy as np
# Define a 2D array with NaN values
arr = np.array([[3, np.nan],
[2, 4]])
# Compute the product along axis 1 while keeping dimensions
result = np.nanprod(arr, axis=1, keepdims=True)
# Print the result
print("Product along axis 1 with keepdims=True:\n", result)
Output:
Product along axis 1 with keepdims=True:
[[ 3.]
[ 8.]]

4. Using the initial
Parameter in nanprod()
The initial
parameter sets a starting value for the product computation.
</>
Copy
import numpy as np
# Define an array with NaN values
arr = np.array([np.nan, 2, 3])
# Compute product with an initial value of 5
result = np.nanprod(arr, initial=5)
# Print the result
print("Product with initial=5:", result)
Output:
Product with initial=5: 30.0

5. Using the where
Parameter in nanprod()
The where
parameter allows selective computation by choosing which elements to include.
</>
Copy
import numpy as np
# Define an array with NaN values
arr = np.array([1, 2, np.nan, 4])
# Define a condition mask
mask = np.array([True, False, True, True])
# Compute product only where mask is True
result = np.nanprod(arr, where=mask)
# Print the result
print("Product with mask:", result)
Output:
Product with mask: 4.0
