Reputation: 21961
I want to remove nans from two arrays if there is a nan in the same position in either of them. The arrays are of same length. Here is what I am doing:
y = numpy.delete(y, numpy.where(numpy.isnan(x)))
numpy.delete(y, numpy.where(numpy.isnan(x)))
However, this only works if x
is the one with nan's. How do I make it work if either x
or y
have nan?
Upvotes: 3
Views: 1235
Reputation: 114310
You have to keep track of the indices to remove from both arrays. You don't need where
since numpy
supports boolean indexing (masks). Also, you don't need delete
since you can just get a subset of the array.
mask = ~np.isnan(x)
x = x[mask]
y = y[mask]
mask = ~np.isnan(y)
x = x[mask]
y = y[mask]
Or more compactly:
mask = ~np.isnan(x) & ~np.isnan(y)
x = x[mask]
y = y[mask]
The first implementation only has an advantage if the arrays are enormous and computing the mask for y
from a smaller array has a performance benefit. In general, I would recommend the second approach.
Upvotes: 3
Reputation: 8090
import numpy as np
import numpy.ma as ma
y = ma.masked_array(y, mask=~np.isnan(x))
y = y.compress() # y without nan where x has nan's
or, after the comments:
mask = ~np.isnan(x) & ~np.isnan(y)
y = ma.masked_array(y, mask=mask)
y = y.compress() # y without nan where x and y have nan's
x = ma.masked_array(x, mask=mask)
x = x.compress() # x without nan where x and y have nan's
or without mask:
mask = ~np.isnan(x) & ~np.isnan(y)
y = y[mask]
x = x[mask]
Upvotes: 1