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?