piRSquared piRSquared - 5 months ago 36
Python Question

invalid value in "less" when comparing np.nan in an array


np.nan < 0


np.array([np.nan]) < 0

array([False], dtype=bool)


np.array([-1, np.nan]) < 0

//anaconda/envs/3.5/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in less
if \__name\__ == '\__main\__':

array([ True, False], dtype=bool)

What does that error mean?


This is only a warning, not an exception. Numpy tries to be helpful and reports that you have a nan in your array. It's safe to ignore it. If the warning itself bothers you (or your users), you can suppress it:

with np.errstate(invalid='ignore'):
    np.less([np.nan, 0], 1)

However, this is really not recommended since it can mask less obvious issues and make debugging harder if you need to find where a nan comes from in a lengthy calculation.

Note the semantics of a nan: nan > 0, nan < 0 and nan < nan, nan == nan are all False.

Also note that in numpy (unlike e.g. pandas) nan really means invalid not not available.