3
aa = np.array([2.0, np.NaN])
aa[aa>1.0] = np.NaN

On running the code above, I get the foll. warning, I understand the reason for this warning, but how to avoid it?

RuntimeWarning: invalid value encountered in greater

1
  • Do you want to replace NaN values in numpy array or just eliminate RuntimeWarning? If the latter, np.seterr(invalid='ignore') is enough. Refer this question Commented Dec 16, 2016 at 12:57

1 Answer 1

1

Store the indices of the valid ones (non - NaNs). First off, we will use these indices to index into the array and perform the comparison to get a mask and then again index into those indices with that mask to retrieve back the indices corresponding to original order. Using the original-ordered indices, we could then assign elements in the input array to NaNs.

Thus, an implementation/solution would be -

idx = np.flatnonzero(~np.isnan(aa))
aa[idx[aa[idx] > 1.0]] = np.nan

Sample run -

In [106]: aa  # Input array with NaNs
Out[106]: array([  0.,   3.,  nan,   0.,   9.,   6.,   6.,  nan,  18.,   6.])

In [107]: idx = np.flatnonzero(~np.isnan(aa)) # Store valid indices

In [108]: idx
Out[108]: array([0, 1, 3, 4, 5, 6, 8, 9])

In [109]: aa[idx[aa[idx] > 1.0]] = np.nan # Do the assignment

In [110]: aa #  Verify
Out[110]: array([  0.,  nan,  nan,   0.,  nan,  nan,  nan,  nan,  nan,  nan])
Sign up to request clarification or add additional context in comments.

3 Comments

In Py3, comparisons with np.nan produce False without warnings.
Does this work for multidimensional arrays? I am getting errors because idx uses linear indexing.
@Darcy For multidim arrays, you would need to use np.where that would give us indices along all dims and then use those to index and assign.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.