How Can I Get All The Index Of All The Nans Of A List?
Solution 1:
If you're using NumPy, you should really start using arrays, and get out of the habit of manually looping at Python level. Manual loops are typically about 100 times slower than letting NumPy handle things, and lists of floats take about 4 times the memory of an array.
In this case, NumPy can give you an array of NaN indices quite simply:
ind = numpy.where(numpy.isnan(a))[0]
numpy.isnan
gives an array of booleans telling which elements of a
are NaN. numpy.where
gives an array of indices of True
elements, but wrapped in a 1-element tuple for consistency with the behavior on multidimensional arrays, so [0]
extracts the array from the tuple.
This works when a
is a list, but you should really use arrays.
Your attempt fails because NaN values aren't equal to each other or themselves:
>>> numpy.nan == numpy.nan
False>>> numpy.nan == float('nan')
False
NaN was designed this way for algorithmic convenience, to make x != x
a simple check for NaN values in environments where producing a NaN to compare against is awkward, and because NaNs have a rarely-used payload component that may be different between different NaNs.
The other answer recommends an is numpy.nan
test, but this is buggy and unreliable. It only works if your NaNs happen to be the specific object numpy.nan
, which is rarely the case:
>>> float('nan') is numpy.nan
False>>> numpy.float64(0)/0is numpy.nan
__main__:1: RuntimeWarning: invalid value encountered in double_scalars
False>>> numpy.array([numpy.nan])[0] is numpy.nan
False
Rely on is numpy.nan
checks, and they will bite you.
Solution 2:
Use np.nan for comparison
import numpy as np
a=[1,2,3,4,np.nan,np.nan,2,np.nan]
ind=[]
for i inrange(0,len(a)):
if a[i] is np.nan:
ind.append(i)
print ind
Output
[4, 5, 7]
Using List Comprehension:
[x for x inrange(0,len(a)) if a[x] is np.nan]
Alternatively, if you have built the array using numpy functions, then use np.isnan(a[i])
for comparison.
Post a Comment for "How Can I Get All The Index Of All The Nans Of A List?"