5

I have a long numpy array with wind direction records, and I'm trying to use numpy's unwrap before running an algorithm to detect jumps in the data. The data contains NaNs, and numpy seems unable to process this. As soon as one NaN is encountered, all following data points returned by unwrap are also converted to NaNs. Is there a way around this?

I think my question boils down to the same question as posted here, but there it's only concluded that the error is related to NaNs in the data and no solution is offered.

Community
  • 1
  • 1
Peter9192
  • 2,899
  • 4
  • 15
  • 24

1 Answers1

9

Assuming you want to keep the NaNs, the simplest solution is to mask out the NaNs before passing the array to unwrap and use the same mask to write the result back:

a[~np.isnan(a)] = np.unwrap(a[~np.isnan(a)])

If you want to keep the original array, use np.copy:

b = np.copy(a)
b[~np.isnan(b)] = np.unwrap(b[~np.isnan(b)])
ecatmur
  • 152,476
  • 27
  • 293
  • 366