- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Return the cumulative sum of array elements treating NaNs as zero in Python

To return the cumulative sum of array elements over a given axis treating NaNs as zero, use the nancumprod() method. 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.

The method returns a new array holding the result unless out is specified, in which it is returned. The result has the same size as a, and the same shape as a if axis is not None or a is a 1-d array. Cumulative works like, 5, 5+10, 5+10+15, 5+10+15+20. The 1st parameter is the input array. The 2nd parameter is the axis along which the cumulative sum is computed. The default (None) is to compute the cumsum over the flattened array.

The 3rd parameter is the type of the returned array and of the accumulator in which the elements are summed. If dtype is not specified, it defaults to the dtype of a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used. The 4th parameter is the alternative output array in which to place the result. It must have the same shape and buffer length as the expected output but the type will be cast if necessary.

## Steps

At first, import the required library −

import numpy as np

Creating a numpy array using the array() method. We have added elements of int type with nan −

arr = np.array([10, 20, 30, 40, np.nan])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

To return the cumulative sum of array elements over a given axis treating NaNs as zero, use the nancumprod() method. 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 −

print("\nCumulative Sum of array elements...\n",np.nancumsum(arr))

## Example

import numpy as np # Creating a numpy array using the array() method # We have added elements of int type with nan arr = np.array([10, 20, 30, 40, np.nan]) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # To return the cumulative sum of array elements over a given axis treating NaNs as zero, use the nancumprod() method # The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. print("\nCumulative Sum of array elements...\n",np.nancumsum(arr))

## Output

Our Array... [10. 20. 30. 40. nan] Dimensions of our Array... 1 Datatype of our Array object... float64 Cumulative Sum of array elements... [ 10. 30. 60. 100. 100.]

- Related Articles
- Return the cumulative sum of array elements over given axis treating NaNs as zero in Python
- Return the cumulative sum of array elements over given axis 0 treating NaNs as zero in Python
- Return the cumulative sum of array elements over given axis 1 treating NaNs as zero in Python
- Return the cumulative sum of array elements treating NaNs as zero but change the type of result in Python
- Return the cumulative product of array elements treating NaNs as one in Python
- Return the cumulative product of array elements over axis 0 treating NaNs as one in Python
- Return the cumulative product of array elements over axis 1 treating NaNs as one in Python
- Return the cumulative product of array elements over a given axis treating NaNs as one in Python
- Return the cumulative product treating NaNs as one but change the type of result in Python
- Cumulative sum of elements in JavaScript
- Return the minimum of an array or minimum ignoring any NaNs in Python
- Return the maximum of an array or maximum ignoring any NaNs in Python
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- Return the maximum of an array with negative infinity or maximum ignoring any NaNs in Python
- Return the minimum of an array along an axis or minimum ignoring any NaNs in Python