Get smallest N values from numpy array ignoring inf and nan

arrays, math, numpy, python

Solution

The only values that should be throwing this out are the negative infinite ones. So try:

import numpy as np
a = np.random.rand(20)
a[4] = -np.inf
k = 10
a[np.isneginf(a)] = inf
result = a[np.argsort(a)[:k]]

Problem

I need a good, quick method for finding the 10 smallest real values from a numpy array that could have arbitrarily many `nan` and/or `inf` values. I need to identify the indices of these smallest real values, not the values themselves. I have found the `argmin` and `nanargmin` functions from numpy. They aren't really getting the job done because I also want to specify more than 1 value, like I want the smallest 100 values, for example. Also they both return `-inf` values as being the smallest value when it is present in the array. `heapq.nsmallest` kind of works, but it also returns `nan` and `-inf` values as smallest values. Also it doesn't give me the indices that I am looking for. Any help here would be greatly appreciated.

Original source