Not sure if it can be done with numpy.sort, but you can use numpy.argsort for sure:
>>> arr
array([[ 105., 4.],
[ 53., 520.],
[ 745., 902.],
[ 19., nan],
[ 184., nan],
[ 22., 10.],
[ 104., 26.]])
>>> arr[np.argsort(arr[:,1])]
array([[ 105., 4.],
[ 22., 10.],
[ 104., 26.],
[ 53., 520.],
[ 745., 902.],
[ 19., nan],
[ 184., nan]])
Answer from alko on Stack Overflow Top answer 1 of 5
7
Not sure if it can be done with numpy.sort, but you can use numpy.argsort for sure:
>>> arr
array([[ 105., 4.],
[ 53., 520.],
[ 745., 902.],
[ 19., nan],
[ 184., nan],
[ 22., 10.],
[ 104., 26.]])
>>> arr[np.argsort(arr[:,1])]
array([[ 105., 4.],
[ 22., 10.],
[ 104., 26.],
[ 53., 520.],
[ 745., 902.],
[ 19., nan],
[ 184., nan]])
2 of 5
5
You can create a masked array:
a = np.loadtxt('test.txt')
mask = np.isnan(a)
ma = np.ma.masked_array(a, mask=mask)
And then sort a using the masked array:
a[np.argsort(ma[:, 1])]
Stack Overflow
stackoverflow.com › questions › 74131398 › ignore-nan-values-when-ranking-using-argsort
numpy - Ignore nan values when ranking using argsort - Stack Overflow
I have a below function that ranks 3rd column of 2D arrays inside of a three-dimensional array. arr[:, :, 3] = arr[:, :, 3].argsort(axis=1)[:, ::-1].argsort(axis=1) + 1 The problem is that nan val...
NumPy
numpy.org › doc › stable › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.4 Manual
As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
GitHub
github.com › pandas-dev › pandas › issues › 12694
BUG: Series.argsort() incorrect handling of NaNs · Issue #12694 · pandas-dev/pandas
March 22, 2016 - It appears that Series.argsort() is implemented as s.dropna().argsort().reindex(s.index, fill_value=-1) = np.argsort(s.dropna()).reindex(s.index, fill_value=-1). There are two problems with this: (a) Since the result represents integer indices into the original series s, the result should not have the same index as s.index -- it should either be a Series with index [0, 1, ...], or more likely simply be a NumPyarray; (b) The way NaNs are effectively removed before calling np.argsort() leads to indexes that are no longer appropriate into the original Series, resulting in the nonsensical results shown in [9] and [10] below.
Author seth-p
Omz Software
omz-software.com › pythonista › numpy › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v1.8 Manual
As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.argsort.html
pandas.Series.argsort — pandas 3.0.1 documentation - PyData |
Argsorts the value, omitting NA/null values, and places the result in the same locations as the non-NA values. ... Unused. Parameter needed for compatibility with DataFrame. kind{‘mergesort’, ‘quicksort’, ‘heapsort’, ‘stable’}, default ‘quicksort’ · Choice of sorting algorithm.
TutorialsPoint
tutorialspoint.com › how-to-leave-nan-as-nan-in-the-pandas-series-argsort-method
How to leave nan as nan in the pandas series argsort method?
March 9, 2022 - If the Series object contains any null values or missing values, then the argsort() method gives -1 to indicate the index of that missing value (Nan value). Unfortunately, the argsort method doesn't have any parameters to skip the Null values. If you want to change the default representation ...
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.argsort.html
pandas.Series.argsort — pandas 2.3.3 documentation
Argsorts the value, omitting NA/null values, and places the result in the same locations as the non-NA values. ... Unused. Parameter needed for compatibility with DataFrame. kind{‘mergesort’, ‘quicksort’, ‘heapsort’, ‘stable’}, default ‘quicksort’ · Choice of sorting algorithm.
Omz Software
omz-software.com › pythonista › numpy › reference › generated › numpy.sort.html
numpy.sort — NumPy v1.8 Manual
If both the real and imaginary parts are non-nan then the order is determined by the real parts except when they are equal, in which case the order is determined by the imaginary parts. Previous to numpy 1.4.0 sorting real and complex arrays containing nan values led to undefined behaviour.
CopyProgramming
copyprogramming.com › howto › using-numpy-argpartition-ignoring-nans
NumPy argpartition: Complete Guide to Partial Sorting, NaN Handling & 2026 Best Practices - Numpy argpartition complete guide to partial sorting
January 9, 2026 - NaN values require explicit handling: Count and exclude them or pre-filter before partitioning to prevent incorrect results. Combine argpartition + argsort for sorted top-K results in O(n + k log k) complexity, delivering 7-25x speedups over pure argsort on large datasets.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.1 Manual
As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
Top answer 1 of 4
7
I think you're probably right -- there is no built in special method to do this. But you could do it in two steps as follows by rolling your NaNs into the place you want them:
a = np.array([ 1., -1., np.nan, 0., np.nan], dtype=np.float32)
sa = np.sort(a)[::-1]
np.roll(sa,-np.count_nonzero(np.isnan(a)))
array([ 1., 0., -1., nan, nan], dtype=float32)
2 of 4
6
What about negating the values twice:
>>> a = np.array([2., -1., nan, 0., nan])
>>> np.sort(a)
array([ -1., 0., 2., nan, nan])
>>> -np.sort(-a)
array([ 2., 0., -1., nan, nan])
NumPy
numpy.org › doc › stable › reference › generated › numpy.sort.html
numpy.sort — NumPy v2.4 Manual
where R is a non-nan real value. Complex values with the same nan placements are sorted according to the non-nan part if it exists.
SciPy
docs.scipy.org › doc › numpy-1.17.0 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v1.17 Manual
As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
NumPy
numpy.org › devdocs › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.5.dev0 Manual
As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.3 Manual
As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
Beautiful Soup
tedboy.github.io › numpy › reference › generated › numpy.argsort.html
11.3.26.1.3. numpy.argsort — Numpy API
As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
SciPy
docs.scipy.org › doc › numpy-1.15.1 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v1.15 Manual
As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.argsort.html
numpy.argsort — NumPy v2.2 Manual
As of NumPy 1.4.0 argsort works with real/complex arrays containing nan values.