Загрузка
>>> np.nan == np.nan # is always False! Use special numpy functions instead.
False
>>> myarr[myarr == np.nan] = 0. # doesn't work
>>> myarr
array([ 1., 0., nan, 3.])
>>> myarr[np.isnan(myarr)] = 0. # use this instead find
>>> myarr
array([1., 0., 0., 3.])
Other related special value functions:
isnan - True if value is nan
isinf - True if value is inf
isfinite - True if not nan or inf
nan_to_num - Map nan to 0, inf to max float, -inf to min float
The following corresponds to the usual functions except that nans are excluded from the results:
>>> x = np.arange(10.)
>>> x[3] = np.nan
>>> x.sum()
nan
>>> np.nansum(x)
42.0