Why can't I suppress numpy warnings
Warnings can often be useful and in most cases I wouldn't advise this, but you can always make use of the Warnings
module to ignore all warnings with filterwarnings
:
warnings.filterwarnings('ignore')
Should you want to suppress uniquely your particular error, you could specify it with:
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')
The warnings controlled by seterr()
are those issued by the numpy ufunc machinery; e.g. when A / B
creates a NaN
in the C code that implements the division, say because there was an inf/inf
somewhere in those arrays. Other numpy code may issue their own warnings for other reasons. In this case, you are using one of the NaN
-ignoring reduction functions, like nanmin()
or the like. You are passing it an array that contains all NaN
s, or at least all NaN
s along an axis that you requested the reduction along. Since the usual reason one uses nanmin()
is to not get another NaN
out, nanmin()
will issue a warning that it has no choice but to give you a NaN
. This goes directly to the standard library warnings
machinery and not the numpy ufunc error control machinery since it isn't a ufunc and this production of a NaN
isn't the same as what seterr(invalid=...)
otherwise deals with.
You may want to avoid suppressing the warning, because numpy raises this for a good reason. If you want to clean up your output, maybe handle it by explicitly returning a pre-defined value when your array is all nan.
def clean_nanmedian(s):
if np.all(np.isnan(s)):
return np.nan
return np.nanmedian(s)
Also, keep in mind that this RuntimeWarning is raised only the first time that this happens in your run-time.