NumPy nancumprod()

The numpy.nancumprod() function computes the cumulative product of an array while treating NaN (Not a Number) values as 1. This ensures that NaN values do not disrupt the cumulative product computation.

Syntax

</>
Copy
numpy.nancumprod(a, axis=None, dtype=None, out=None)

Parameters

ParameterTypeDescription
aarray_likeInput array containing numbers, including possible NaN values.
axisint, optionalThe axis along which the cumulative product is computed. If None, the input array is flattened.
dtypedtype, optionalThe data type of the returned array and accumulator. Defaults to the input array’s dtype unless precision adjustments are required.
outndarray, optionalAn optional array to store the output. Must have the same shape as the expected output.

Return Value

Returns an array with the cumulative product of elements along the specified axis, treating NaN values as 1. If out is specified, the result is stored in the provided output array.


Examples

1. Computing the Cumulative Product with NaN Values

Here, we compute the cumulative product of an array containing NaN values.

</>
Copy
import numpy as np

# Define an array with NaN values
arr = np.array([2, np.nan, 3, 4, np.nan, 5])

# Compute the cumulative product treating NaNs as 1
result = np.nancumprod(arr)

# Print the results
print("Input array:", arr)
print("Cumulative product ignoring NaNs:", result)

Output:

Input array: [ 2. nan  3.  4. nan  5.]
Cumulative product ignoring NaNs: [  2.   2.   6.  24.  24. 120.]

2. Using the axis Parameter in nancumprod()

We compute the cumulative product along a specified axis.

</>
Copy
import numpy as np

# Define a 2D array with NaN values
arr = np.array([[1, np.nan, 2], 
                [np.nan, 3, 4]])

# Compute the cumulative product along axis 0 (column-wise)
result_axis0 = np.nancumprod(arr, axis=0)

# Compute the cumulative product along axis 1 (row-wise)
result_axis1 = np.nancumprod(arr, axis=1)

# Print the results
print("Input array:\n", arr)
print("\nCumulative product along axis 0:\n", result_axis0)
print("\nCumulative product along axis 1:\n", result_axis1)

Output:

Input array:
 [[ 1. nan  2.]
 [nan  3.  4.]]

Cumulative product along axis 0:
 [[ 1. nan  2.]
 [ 1.  3.  8.]]

Cumulative product along axis 1:
 [[ 1.  1.  2.]
 [ 1.  3. 12.]]

3. Using the out Parameter

Storing the cumulative product in an output array.

</>
Copy
import numpy as np

# Define an array with NaN values
arr = np.array([1, 2, np.nan, 4, 5])

# Create an output array with the same shape
output_array = np.empty_like(arr)

# Compute the cumulative product and store it in output_array
np.nancumprod(arr, out=output_array)

# Print the results
print("Output array storing the cumulative product:", output_array)

Output:

Output array storing the cumulative product: [ 1.  2.  2.  8. 40.]

4. Handling All NaN Values

When all elements in a slice are NaN, the function returns an array of ones.

</>
Copy
import numpy as np

# Define an array consisting only of NaN values
arr = np.array([np.nan, np.nan, np.nan])

# Compute the cumulative product
result = np.nancumprod(arr)

# Print the results
print("Cumulative product of an all-NaN array:", result)

Output:

Cumulative product of an all-NaN array: [1. 1. 1.]

Since all elements are NaN, numpy.nancumprod() returns an array of ones instead of propagating NaN values.