1

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.

1 Answer 1

4

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]
Sign up to request clarification or add additional context in comments.

1 Comment

This was exactly what I needed! I was already using the ~np.isnan(), but I hadn't thought to tell the time array to index using the flux array. That really should have been obvious. +1 for a simple solution to the problem!

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.