The numpy.nancumsum()
function in NumPy is used to return the cumulative sum of elements in a given array over a given axis in such a way that NaN
values are treated as zeroes.
numpy.nancumsum(a, axis=None, dtype=None, out=None)
The numpy.nancumsum()
function takes the following parameter values:
a
: This is the input array containing numbers to be computed. This is a required parameter.axis
: This is the axis along which the product is determined. This is an optional parameter.dtype
: This is the data type of the output array. This is an optional parameter.out
: This is the alternate array where the result is placed. This is an optional parameter.The numpy.nancumsum()
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 nancumsum() functionmyarray = np.nancumsum(x, axis=0)print(x)print(myarray)
numpy
module.x
, using the array()
method.np.nancumsum()
function on the array. The result is assigned to a variable myarray
.x
.myarray
.Note: The
NaN
values are treated as0
.