HolyMonk HolyMonk - 29 days ago 9
Python Question

The logic behind `np.nanargmin([np.nan, np.inf]) = 0`

I understand that

np.nanargmin
finds the smallest number in a list that is not a NaN. However if called upon the array
[np.nan, np.inf]
it yields
0
which is a NaN. I find this behavior rather odd and am just wondering what the logic in defining
np.argmin
this way.

Answer Source

If you look at the documentation for np.nanargmin it says:

Warning: the results cannot be trusted if a slice contains only NaNs and Infs.

If you view the source code it has the following line:

a, mask = _replace_nan(a, np.inf)

So it is replacing all nan occurrences with inf, and so then it is finding the min (still a bit questionable), which will be argmin([inf, inf]).