Palmetto_Girl86
Palmetto_Girl86

Reputation: 1007

Removing Corresponding Entries from Two Numpy Arrays

I have what I'm quite sure is a simple question, but I'm not having much luck finding an explanation online.

I have an array of flux values and a corresponding array of time values. Obviously those two arrays are one-to-one (one flux value for each time value). However, some of my flux values are NaNs.

My question is this: How do I remove the corresponding values from the time array when I remove the NaNs from the flux array?

These arrays are large enough (several thousand entries) that it would be exceedingly cumbersome to do it by hand.

Upvotes: 1

Views: 97

Answers (1)

xnx
xnx

Reputation: 25478

You could try boolean indexing:

In [13]: time
Out[13]: array([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

In [15]: flux
Out[15]: array([  1.,   1.,   1.,   1.,   1.,  nan,  nan,  nan,   1.,   1.,   1.])


In [16]: time2 = time[~np.isnan(flux)]
In [17]: flux2 = flux[~np.isnan(flux)]  
In [18]: time2
Out[18]: array([  0.,   1.,   2.,   3.,   4.,   8.,   9.,  10.])

In [19]: flux2
Out[19]: array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])

Just write time = time[~np.isnan(flux)] etc. if you don't need the original arrays any more.

A more complicated way is to use masked arrays:

In [20]: m = np.ma.masked_invalid(flux)
In [21]: time2 = time[~m.mask]
In [22]: time2
Out[22]: array([  0.,   1.,   2.,   3.,   4.,   8.,   9.,  10.])

In [23]: flux2
Out[23]: array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
In [22]: flux2 = flux[~m.mask]

Upvotes: 4

Related Questions