Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing nan in array at position from another numpy array

Tags:

python

numpy

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?

like image 834
user308827 Avatar asked May 24 '26 19:05

user308827


2 Answers

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.

like image 88
Mad Physicist Avatar answered May 26 '26 08:05

Mad Physicist


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]
like image 33
orange Avatar answered May 26 '26 09:05

orange



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!