均值、南均值和警告: 空切片均值

假设我构造了两个数字数组:

a = np.array([np.NaN, np.NaN])
b = np.array([np.NaN, np.NaN, 3])

现在我发现 np.mean对于 ab都返回 nan:

>>> np.mean(a)
nan
>>> np.mean(b)
nan

自从 numpy 1.8(2016年4月20日发布)以来,我们一直很幸运地使用了 Nanmean,它忽略了 nan值:

>>> np.nanmean(b)
3.0

但是,当数组没有 但是 nan值时,它会发出警告:

>>> np.nanmean(a)
nan
C:\python-3.4.3\lib\site-packages\numpy\lib\nanfunctions.py:598: RuntimeWarning: Mean of empty slice
warnings.warn("Mean of empty slice", RuntimeWarning)

我不喜欢压制警告; 有没有更好的功能,我可以用来得到 nanmean的行为没有警告?

135150 次浏览

A NaN value is defined to not be equal to itself:

>>> float('nan') == float('nan')
False
>>> np.NaN == np.NaN
False

You can use a Python conditional and the property of a nan never being equal to itself to get this behavior:

>>> a = np.array([np.NaN, np.NaN])
>>> b = np.array([np.NaN, np.NaN, 3])
>>> np.NaN if np.all(a!=a) else np.nanmean(a)
nan
>>> np.NaN if np.all(b!=b) else np.nanmean(b)
3.0

You can also do:

import warnings
import numpy as np


a = np.array([np.NaN, np.NaN])
b = np.array([np.NaN, np.NaN, 3])


with warnings.catch_warnings():
warnings.filterwarnings('error')
try:
x=np.nanmean(a)
except RuntimeWarning:
x=np.NaN
print x

I really can't see any good reason not to just suppress the warning.

The safest way would be to use the warnings.catch_warnings context manager to suppress the warning only where you anticipate it occurring - that way you won't miss any additional RuntimeWarnings that might be unexpectedly raised in some other part of your code:

import numpy as np
import warnings


x = np.ones((1000, 1000)) * np.nan


# I expect to see RuntimeWarnings in this block
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
foo = np.nanmean(x, axis=1)

@dawg's solution would also work, but ultimately any additional steps that you have to take in order to avoid computing np.nanmean on an array of all NaNs are going to incur some extra overhead that you could avoid by just suppressing the warning. Also your intent will be much more clearly reflected in the code.

I have got this runtime warning when I perform np.nanmean over a 3-D array, e.g. (time, lon, lat). Maybe not a direct answer to your question, but I would like to add this warning message in my case can be related to point cell value with all NaN value series.