Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return the cumulative sum of array elements treating NaNs as zero but change the type of result in Python
To return the cumulative sum of array elements over a given axis treating NaNs as zero, use the numpy.nancumsum() 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.
Syntax
numpy.nancumsum(a, axis=None, dtype=None, out=None)
Parameters
The function accepts the following parameters:
- a − Input array
- axis − Axis along which the cumulative sum is computed. Default is None (flattened array)
- dtype − Type of the returned array. If not specified, defaults to input array's dtype
- out − Alternative output array to place the result
Example
Let's create a 2D array with NaN values and calculate the cumulative sum ?
import numpy as np
# Creating a numpy array with NaN values
arr = np.array([[10, 20, 30], [40, np.nan, 60]])
# Display the array
print("Our Array...")
print(arr)
# Check dimensions and datatype
print("\nDimensions:", arr.ndim)
print("Datatype:", arr.dtype)
# Calculate cumulative sum treating NaNs as zero
print("\nCumulative Sum along axis=1:")
result = np.nancumsum(arr, axis=1, dtype=int)
print(result)
Our Array... [[10. 20. 30.] [40. nan 60.]] Dimensions: 2 Datatype: float64 Cumulative Sum along axis=1: [[ 10 30 60] [ 40 40 100]]
Different Axis Examples
Here's how nancumsum() works with different axis values ?
import numpy as np
arr = np.array([[10, np.nan, 30], [40, 50, np.nan]])
print("Original array:")
print(arr)
# Cumulative sum along axis=0 (columns)
print("\nAxis=0 (along columns):")
print(np.nancumsum(arr, axis=0))
# Cumulative sum along axis=1 (rows)
print("\nAxis=1 (along rows):")
print(np.nancumsum(arr, axis=1))
# Flattened cumulative sum
print("\nFlattened (axis=None):")
print(np.nancumsum(arr))
Original array: [[10. nan 30.] [40. 50. nan]] Axis=0 (along columns): [[10. nan 30.] [50. 50. 30.]] Axis=1 (along rows): [[10. 10. 40.] [40. 90. 90.]] Flattened (axis=None): [10. 10. 40. 80. 130. 130.]
Changing Output Data Type
You can specify different data types for the output array ?
import numpy as np
arr = np.array([[1.5, np.nan, 3.7], [4.2, 5.8, np.nan]])
print("Original array (float):")
print(arr)
# Convert to integer output
print("\nAs integer:")
print(np.nancumsum(arr, axis=1, dtype=int))
# Keep as float
print("\nAs float:")
print(np.nancumsum(arr, axis=1, dtype=float))
Original array (float): [[1.5 nan 3.7] [4.2 5.8 nan]] As integer: [[1 1 4] [4 9 9]] As float: [[1.5 1.5 5.2] [4.2 10. 10. ]]
Conclusion
Use numpy.nancumsum() to calculate cumulative sums while treating NaN values as zero. The dtype parameter allows you to control the output data type, and the axis parameter determines the direction of cumulation.
