user308827
user308827

Reputation: 21961

Removing nan in array at position from another numpy array

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

Answers (2)

Mad Physicist
Mad Physicist

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

orange
orange

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

Related Questions