The numpy.nancumprod()
function in NumPy is used to return the cumulative product of each element of an input array over a given axis in such a way that NaN
values are treated as 1
.
numpy.nancumprod(a, axis=None, dtype=None, out=None)
The numpy.nancumprod()
function takes the following parameter values:
a
(required): This is the input array containing numbers to be computed.axis
(optional): This is the axis along which the product is determined.dtype
(optional): This is the data type of the output array.out
(optional): This is the alternate array where the result is placed.The numpy.nancumprod()
function returns an output array holding the result.
import numpy as np# creating an arrayx = np.array([1, 2, np.nan, 2, np.nan])# Implementing the nancumprod() functionmyarray = np.nancumprod(x, axis=0)print(x)print(myarray)
numpy
module.x
, using the array()
method.np.nancumprod()
function on the array. The result is assigned to a variable, myarray
.x
.myarray
.Note: It is worth remembering that
NaN
values are treated as1
.