sort descending with NaNs
Hello, when one has to find a given number of highest values in an array containing NaNs, the sort function (always ascending) is uncomfortable. Since numpy >= 1.4.0 NaNs are sorted to the end, so the searched values are just before the first NaN in a unpredictable position and one has to do another search for the first NaN position. Sorting descending will solve the problem, but there is no option with numpy sort. There is any other trick to avoid this second search? TIA, Fabrizio Pollastri
On Thu, Jan 27, 2011 at 9:50 AM, Fabrizio Pollastri
Hello,
when one has to find a given number of highest values in an array containing NaNs, the sort function (always ascending) is uncomfortable.
Since numpy >= 1.4.0 NaNs are sorted to the end, so the searched values are just before the first NaN in a unpredictable position and one has to do another search for the first NaN position.
Sorting descending will solve the problem, but there is no option with numpy sort. There is any other trick to avoid this second search?
If you just want to reverse the result, try a[::-1]. I think you may still need to find the boundaries of the nan's just to make sure they aren't included among the largest values. Searchsorted is pretty quick in any case. Chuck
On Thu, Jan 27, 2011 at 10:10 AM, Charles R Harris < charlesr.harris@gmail.com> wrote:
On Thu, Jan 27, 2011 at 9:50 AM, Fabrizio Pollastri
wrote: Hello,
when one has to find a given number of highest values in an array containing NaNs, the sort function (always ascending) is uncomfortable.
Since numpy >= 1.4.0 NaNs are sorted to the end, so the searched values are just before the first NaN in a unpredictable position and one has to do another search for the first NaN position.
Sorting descending will solve the problem, but there is no option with numpy sort. There is any other trick to avoid this second search?
If you just want to reverse the result, try a[::-1]. I think you may still need to find the boundaries of the nan's just to make sure they aren't included among the largest values. Searchsorted is pretty quick in any case.
To sort in descending order sort the negatives, i.e. In [1]: -sort(-array((0,1,2,3,4,nan))) Out[1]: array([ 4., 3., 2., 1., 0., nan]) I still think a.searchsorted(nan) would be faster. Chuck
participants (2)
-
Charles R Harris
-
Fabrizio Pollastri