0

It seems like when "nan" values are included in a Numpy array, the array cannot be sorted though sorted(). Here is an example:

import numpy as np

values = np.array([float('nan'),2,float('nan'),1],dtype=float)
valuesSorted = sorted(values)
print(valuesSorted)

The output is [nan, 2.0, nan, 1.0]. Whereas if you sort [2,1] with the same code, the output is [1.0,2.0].

Alternatively, if you use values.sort():

import numpy as np

values = np.array([float('nan'),2,float('nan'),1],dtype=float)
values.sort()
print(values)

The output is: [ 1. 2. nan nan], meaning that sort() can sort arrays that include nan values.

My questions are: why does the function sorted() not sort an array containing nan values? Is it possible to use sorted() to sort an array containing nan values?

MakotoE
  • 1,814
  • 1
  • 20
  • 39
  • https://stackoverflow.com/questions/4240050/python-sort-function-breaks-in-the-presence-of-nan – Pythonista May 31 '18 at 23:07
  • 2
    As for why it works in Numpy - from the [docs](https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html): _Previous to numpy 1.4.0 sorting real and complex arrays containing nan values led to undefined behaviour. In numpy versions >= 1.4.0 nan values are sorted to the end._ – miradulo May 31 '18 at 23:08
  • @Pythonista Ah, I didn't know the right keywords to search for. I also just realized that `sorted()` is not implemented by Numpy (which I thought was the case), instead the underlying list is passed to the builtin function. – MakotoE May 31 '18 at 23:10
  • 1
    @ME914 there is no underlying list, but sorted creates a list out of the iterable you pass to it. – juanpa.arrivillaga Jun 01 '18 at 00:08

0 Answers0