1

I have thousands of files with irregularly data that I am trying to interpolate to the same grid. The data is stored in individual numpy arrays for a number of different variables and times. I have been interpolating the times using matplotlib.dates by converting times to numbers. But because I don't want to extrapolate missing data the interpolation has introduced np.nan values in the dataset that makes the conversion from matplotlib date to datetime object tricky because I need to conserve shape (i.e. can't remove np.nans) but md.num2date cannot handle masked arrays or np.nan.

Is there another command like md.date2num and md.num2date in another library that can account for nans? Or is the only solution here a for loop?

import numpy as np
import matplotlib.dates as md

tdata = np.array([np.nan, np.nan, 736926.9827155,736926.98274578,736926.98276768,736926.98285067,736926.9828712])

#Allows num2date to work but removes the nan values and won't conserve shape
test1 = md.num2date(tdata[~np.isnan(tdata)])

print(len(tdata),len(test1))

#Using a mask will produce an error 
#test2 = md.num2date(np.ma.masked_array(tdata,np.isnan(tdata)))

Note that I need the shapes to be the same and that np.nan could occur anywhere in the array. I am using python 2.7

1 Answer 1

2

Maybe something like this?

test1 = np.full_like(tdata, np.nan, dtype=object)
test1[~np.isnan(tdata)] = md.num2date(tdata[~np.isnan(tdata)])
Sign up to request clarification or add additional context in comments.

Comments

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.