python - Reverse sort of Numpy array with NaN values -


i have numpy array nan values:

>>> array([  1.,  -1.,   nan,  0.,  nan], dtype=float32) 

i can sort in ascending or 'descending' order:

>>> numpy.sort(a) array([ -1.,   0.,   1.,  nan,  nan], dtype=float32) >>> numpy.sort(a)[::-1] array([ nan,  nan,   1.,   0.,  -1.], dtype=float32) 

however, want descending order nan values @ end, this:

>>> numpy.genuine_reverse_sort(a) array([ 1.,   0.,   -1.,  nan,  nan], dtype=float32) 

how accomplished? suspect there no special method this.

i think you're right -- there no built in special method this. in 2 steps follows rolling nans place 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) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -