Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
FutureWarning: In Numpy versions <= 1.8 Nan is returned for slices that are all-NaN or empty. In later versions zero will be returned.
Parameters: | a : array_like
axis : int, optional
dtype : data-type, optional
out : ndarray, optional
keepdims : bool, optional
|
---|---|
Returns: | y : ndarray or numpy scalar |
See also
Notes
If both positive and negative infinity are present, the sum will be Not A Number (NaN).
Numpy integer arithmetic is modular. If the size of a sum exceeds the size of an integer accumulator, its value will wrap around and the result will be incorrect. Specifying dtype=double can alleviate that problem.
Examples
>>> np.nansum(1)
1
>>> np.nansum([1])
1
>>> np.nansum([1, np.nan])
1.0
>>> a = np.array([[1, 1], [1, np.nan]])
>>> np.nansum(a)
3.0
>>> np.nansum(a, axis=0)
array([ 2., 1.])
>>> np.nansum([1, np.nan, np.inf])
inf
>>> np.nansum([1, np.nan, np.NINF])
-inf
>>> np.nansum([1, np.nan, np.inf, -np.inf]) # both +/- infinity present
nan