Return the cumulative product of array elements over a given axis treating NaNs as one in Python

To return the cumulative product of array elements over a given axis treating NaNs as one, use the nancumprod() method. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones. Ones are returned for slices that are all-NaN or empty.

Cumulative product works like: 5, 5×10, 5×10×15, 5×10×15×20. When NaN values are present, they are treated as 1, so the cumulative product continues without interruption.

Syntax

numpy.nancumprod(a, axis=None, dtype=None, out=None)

Parameters

The nancumprod() method accepts the following parameters ?

  • a ? Input array
  • axis ? Axis along which the cumulative product is computed. By default the input is flattened
  • dtype ? Type of the returned array and accumulator. If not specified, defaults to array's dtype
  • out ? Alternative output array to place the result

Example

Let's create a 2D array with NaN values and compute the cumulative product ?

import numpy as np

# Creating a numpy array with NaN values
arr = np.array([[5, 10, 15], [20, np.nan, 30]])

# Display the array
print("Our Array...")
print(arr)

# Check the dimensions
print("\nDimensions of our Array...")
print(arr.ndim)

# Get the datatype
print("\nDatatype of our Array object...")
print(arr.dtype)

# Cumulative product treating NaNs as 1
print("\nCumulative Product along axis=1...")
print(np.nancumprod(arr, axis=1))
Our Array...
[[ 5. 10. 15.]
 [20. nan 30.]]

Dimensions of our Array...
2

Datatype of our Array object...
float64

Cumulative Product along axis=1...
[[  5.  50. 750.]
 [ 20.  20. 600.]]

Different Axis Examples

Let's see how nancumprod() works with different axis values ?

import numpy as np

arr = np.array([[2, np.nan, 4], [3, 5, np.nan], [1, 2, 6]])
print("Original Array:")
print(arr)

# Along axis=0 (columns)
print("\nCumulative product along axis=0:")
print(np.nancumprod(arr, axis=0))

# Along axis=1 (rows)
print("\nCumulative product along axis=1:")
print(np.nancumprod(arr, axis=1))

# Flattened array (default)
print("\nCumulative product (flattened):")
print(np.nancumprod(arr))
Original Array:
[[ 2. nan  4.]
 [ 3.  5. nan]
 [ 1.  2.  6.]]

Cumulative product along axis=0:
[[ 2. nan  4.]
 [ 6.  5.  4.]
 [ 6. 10. 24.]]

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

Cumulative product (flattened):
[ 2.  2.  8. 24. 120. 120. 120. 240. 1440.]

Handling All-NaN Slices

When a slice contains only NaN values, the result is 1 ?

import numpy as np

# Array with all-NaN row
arr = np.array([[1, 2, 3], [np.nan, np.nan, np.nan], [4, 5, 6]])
print("Array with all-NaN row:")
print(arr)

print("\nCumulative product along axis=1:")
print(np.nancumprod(arr, axis=1))
Array with all-NaN row:
[[ 1.  2.  3.]
 [nan nan nan]
 [ 4.  5.  6.]]

Cumulative product along axis=1:
[[ 1.  2.  6.]
 [ 1.  1.  1.]
 [ 4. 20. 120.]]

Key Points

  • NaN values are treated as 1 in the cumulative product calculation
  • Leading NaNs in a slice are replaced by 1
  • All-NaN slices return 1 for each position
  • The function preserves array shape when axis is specified
  • Default behavior flattens the array before computing cumulative product

Conclusion

The numpy.nancumprod() function is useful for computing cumulative products while handling NaN values gracefully. It treats NaN as 1, allowing calculations to continue without interruption, making it ideal for datasets with missing values.

---
Updated on: 2026-03-26T20:09:04+05:30

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements