numpy.nancumsum() in Python

numpy.nancumsum() function is used when we want to compute the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. Zeros are returned for slices that are all-NaN or empty.

Syntax : numpy.nancumsum(arr, axis=None, dtype=None, out=None)

Parameters :
arr : [array_like] Array containing numbers whose cumulative sum is desired. If arr is not an array, a conversion is attempted.
axis : Axis along which the cumulative sum is computed. The default is to compute the sum of the flattened array.
dtype : Type of the returned array, as well as of the accumulator in which the elements are multiplied. If dtype is not specified, it defaults to the dtype of arr, unless arr has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used instead.
out : [ndarray, optional] A location into which the result is stored.
-> If provided, it must have a shape that the inputs broadcast to.
-> If not provided or None, a freshly-allocated array is returned.

Return : A new array holding the result is returned unless out is specified, in which case it is returned.

Code #1 : Working

 # Python program explaining # numpy.nancumsum() function    import numpy as geek in_num = 10    print ("Input  number : ", in_num)      out_sum = geek.nancumsum(in_num)  print ("cumulative sum of input number : ", out_sum)

Output :

Input  number :  10
cumulative sum of input number :  

Code #2 :

 # Python program explaining # numpy.nancumsum() function    import numpy as geek    in_arr = geek.array([[2, 4, 6], [1, 3, geek.nan]])     print ("Input array : ", in_arr)       out_sum = geek.nancumsum(in_arr)  print ("cumulative sum of array elements: ", out_sum)

Output :

Input array :  [[  2.   4.   6.]
[  1.   3.  nan]]
cumulative sum of array elements:  [  2.   6.  12.  13.  16.  16.]

Code #3 :

 # Python program explaining # numpy.nancumsum() function    import numpy as geek    in_arr = geek.array([[2, 4, 6], [1, 3, geek.nan]])     print ("Input array : ", in_arr)       out_sum = geek.nancumsum(in_arr, axis = 0)  print ("cumulative sum of array elements taking axis 0: ", out_sum)

Output :

Input array :  [[  2.   4.   6.]
[  1.   3.  nan]]
cumulative sum of array elements taking axis 0:  [[ 2.  4.  6.]
[ 3.  7.  6.]]

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.