dpnp.nansum

dpnp.nansum(a, axis=None, dtype=None, out=None, keepdims=False, initial=None, where=True)[source]

Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.

For full documentation refer to numpy.nansum.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}) -- Input array.

  • axis ({None, int or tuple of ints}, optional) -- Axis or axes along which the sum is computed. The default is to compute the sum of the flattened array. Default: None.

  • dtype ({None, dtype}, optional) -- The type of the returned array and of the accumulator in which the elements are summed. By default, the dtype of a is used. An exception is when a has an integer type with less precision than the platform (u)intp. In that case, the default will be either (u)int32 or (u)int64 depending on whether the platform is 32 or 64 bits. For inexact inputs, dtype must be inexact. Default: None.

  • out ({None, dpnp.ndarray, usm_ndarray}, optional) -- Alternate output array in which to place the result. If provided, it must have the same shape as the expected output, but the type will be cast if necessary. The casting of NaN to integer can yield unexpected results. Default: None.

  • keepdims ({None, bool}, optional) -- If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a. Default: False.

Returns:

out -- A new array holding the result is returned 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.

Return type:

dpnp.ndarray

Limitations

Parameters initial and where are supported with their default values. Otherwise NotImplementedError exception will be raised.

See also

dpnp.sum

Sum across array propagating NaNs.

dpnp.isnan

Show which elements are NaN.

dpnp.isfinite

Show which elements are not NaN or +/-inf.

Notes

If both positive and negative infinity are present, the sum will be Not A Number (NaN).

Examples

>>> import dpnp as np
>>> np.nansum(np.array([1]))
array(1)
>>> np.nansum(np.array([1, np.nan]))
array(1.)
>>> a = np.array([[1, 1], [1, np.nan]])
>>> np.nansum(a)
array(3.)
>>> np.nansum(a, axis=0)
array([2.,  1.])
>>> np.nansum(np.array([1, np.nan, np.inf]))
array(inf)
>>> np.nansum(np.array([1, np.nan, np.NINF]))
array(-inf)
>>> # both +/- infinity present
>>> np.nansum(np.array([1, np.nan, np.inf, -np.inf]))
array(nan)