How does python's max function handle np.nan?

Viewed 51

Recently I stumbled over this quite unintuitive behaviour:

import numpy as np
max([0, np.nan])  # 0 
max([np.nan, 0.0])  # np.nan

I assume the max function sees both entries as maximal and - according to the documentation - returns the first one. I have to admit, that I do not know how the Python function max knows what to do with numpy types in general. Of course, np.max returns the expected result (np.nan). I am using Python 3. Does someone know what's going on here?

1 Answers

Because you are using the native python max() function on numpy NaN values, the behaviour is not obvious. I found this question and the corresponding answer:

The reason is that max works by taking the first value as the "max seen so far", and then checking each other value to see if it is bigger than the max seen so far. But nan is defined so that comparisons with it always return False --- that is, nan > 1 is false but 1 > nan is also false.

So if you start with nan as the first value in the array, every subsequent comparison will be check whether some_other_value > nan. This will always be false, so nan will retain its position as "max seen so far". On the other hand, if nan is not the first value, then when it is reached, the comparison nan > max_so_far will again be false. But in this case that means the current "max seen so far" (which is not nan) will remain the max seen so far, so the nan will always be discarded.

Related