nan_helper

tayph.nan_helper(y)[source] [edit on github]

Helper function to handle indices and logical indices of NaNs.

Input:
  • y, 1D (!!) numpy array with possible NaNs

Output:
  • nans, logical indices of NaNs

  • index, a function, with signature indices= index(logical_indices), to convert logical indices of NaNs to ‘equivalent’ indices

Example:
>>> # linear interpolation of NaNs
>>> import numpy as np
>>> y = np.array([0,0.1,0.2,np.nan,0.4,0.5])
>>> nans, x= nan_helper(y)
>>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])