max(float('nan'), 1) evaluates to nan
max(1, float('nan')) evaluates to 1
Is it the intended behavior?
Thanks for the answers.
max raises an exception when the iterable is empty. Why wouldn’t Python’s max raise an exception when nan is present? Or at least do something useful, like return nan or ignore nan. The current behavior is very unsafe and seems completely unreasonable.
I found an even more surprising consequence of this behavior, so I just posted a related question.
The float
nanis neither bigger nor smaller than the integer1.maxstarts by choosing the first element, and only replaces it when it finds an element which is strictly larger.Since
nanis not larger than 1, 1 is returned.Since 1 is not larger than
nan,nanis returned.PS. Note that
np.maxtreatsfloat('nan')differently:but if you wish to ignore
np.nans, you can usenp.nanmax: