Say I construct two numpy arrays:
a = np.array([np.NaN, np.NaN])
b = np.array([np.NaN, np.NaN, 3])
Now I find that np.mean
returns nan
for both a
and b
:
>>> np.mean(a)
nan
>>> np.mean(b)
nan
Since numpy 1.8 (released 20 April 2016), we've been blessed with nanmean, which ignores nan
values:
>>> np.nanmean(b)
3.0
However, when the array has nothing but nan
values, it raises a warning:
>>> np.nanmean(a)
nan
C:python-3.4.3libsite-packages
umpylib
anfunctions.py:598: RuntimeWarning: Mean of empty slice
warnings.warn("Mean of empty slice", RuntimeWarning)
I don't like suppressing warnings; is there a better function I can use to get the behaviour of nanmean
without that warning?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…