Search code examples
pythonlistnumpyindexing

Finding the index of items containing 'inf' or 'nan'


The following is a sample of 1 item in my list:

array([[  1,   2,   3,
          43,   83,   92],
       [  12,   54,   93,
          23,   94,   83],
       [  23,   inf,   inf,
          inf,   inf,   inf],
       [  83,   33,   33,
          83,   13,   83],
       [  83,   nan,   83,
          73,   43,   43],
       [  43,   83,   93,
          22,   83,   54],
       [  66,   nan,   74,
          84,   84,   75],
       [  74,   44,   65,
          6,   9,   7],
       [  54,   9,   74,
          754,   55,   74]])

Some of those items contain inf or nan values. I'm thus trying to return the index of the items that contain such values. I thus tried doing the following:

for x in f:
    if float('inf') in x:
        idx.append(f.index(x))
    elif float('nan') in x:
        idx.append(f.index(x))

When I run my script however, I get the following error:

idx.append(f.index(x))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Why is that? How can I solve this issue?

As a reminder, the above sample is only 1 item. Since it contains inf and nan, I would like to return the index of that item. The item is basically a list of lists.

Thanks.


Solution

  • Following suggestion by @coldspeed you can obtain an index of elements containing infs or nans as

    idx = [i for i, arr in enumerate(f) if not np.isfinite(arr).all()]

    There is a gotcha coming from idx.append(f.index(x)) that results in your error. It will return True if x is the first element of f and throw an error otherwise. It will also throw an error if you set f[0] = x.copy() and attempt to check x in f. Both are due to the fact that bool(x == x) is not well-defined.