We can easily get the sum of a NumPy array but if there is any NA value, then the sum is NA too. In this case, we can use the nansum
method. For example.
import numpy as np my_arr = [1,2,np.nan] my_arr
[1, 2, nan]
Let’s try to get the sum.
np.sum(my_arr)
nan
As we can see, we got nan
. If we want to get the sum of the NumPy array by ignoring the NA values, we can work as follows:
np.nansum(my_arr)
3.0
Voilà! We got the required result.