2

I am trying to sort a list of floats (and nan values) in python like:

print max(list)
list.sort()
list.reverse()
for i in range(100):
    print list[i]
nan
nan
2.0803176458
nan
nan
23.1620761136
15.9680303803
15.3134388394
14.68055076
11.450492644
8.96268420227
8.15331554187
5.24420616524
3.9665322752
3.69758305442
1.08500491226
-0.227894225141
-0.254784399765
-0.866879940573
-1.21267324819
-2.21811678021
nan
nan
2.69325878444

when I run it I get different values for the max most times and my list is not sorted when it prints out (results are under the code above)

Does anyone have any insight as to why this is happening?

Alex Brashear
  • 864
  • 3
  • 9
  • 15

2 Answers2

5
>>> float('nan') < 3.14
False
>>> float('nan') > 3.14
False
>>> float('nan') < float('nan')
False
>>> float('nan') > float('nan')
False
>>> float('nan') == float('nan')
False

The problem is that nan doesn't have an ordering with respect to other numbers. Comparisons always return False, whether that's against numbers or other nan values. The contract for sort() requires that list items have a consistent ordering in order to sort correctly.

To sort nan you'll need to override this behavior and define how you want nan to sort. For instance, if you want it to come before all other numbers you could use a key function to make nan equivalent to -inf.

>>> l = [float('nan'), 3.14, -1, 0, float('nan')]
>>> import math
>>> sorted(l, key=lambda f: float('-inf') if math.isnan(f) else f)
[nan, nan, -1, 0, 3.14]
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
3

That's because a nan can't be compared to any other object(not even itself).

>>> x = float('nan')
>>> y = x
>>> y == x
False

Use numpy to handle nan's properly:

>>> import numpy as np
>>> arr = np.array(nums)
>>> np.sort(arr)
array([ -2.21811678,  -1.21267325,  -0.86687994,  -0.2547844 ,
        -0.22789423,   1.08500491,   2.08031765,   2.69325878,
         3.69758305,   3.96653228,   5.24420617,   8.15331554,
         8.9626842 ,  11.45049264,  14.68055076,  15.31343884,
        15.96803038,  23.16207611,          nan,          nan,
                nan,          nan,          nan,          nan])
>>> np.nanmax(arr)
23.162076113600001
>>> np.nanmin(arr)
-2.2181167802099999
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504