Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolate only one value of a TimeSerie using Python / Pandas

Tags:

python

pandas

I have a pandas.core.series.TimeSeries named ts like this:

timestamp
2013-08-11 14:23:50        0.3219
2013-08-11 14:23:49        0.3222
2013-08-11 14:19:14        0.3305
2013-08-11 00:47:15        0.3400
2013-08-11 00:47:15.001    0.3310
2013-08-11 00:47:15.002    0.3310
2013-08-10 22:38:15.003    0.3400
2013-08-10 22:38:14        0.3403
2013-08-10 22:38:13        0.3410

Index of this TimeSerie are irregularly spaced.

I would like to have value of ts for a given datetime such as 2013-08-11 14:20:00

I just need to interpolate ONE value, not the whole TimeSerie

I just want to interpolate data using a linear function between the previous index (2013-08-11 14:23:49) and the next index (2013-08-11 14:19:14)

like image 406
working4coins Avatar asked Aug 12 '13 07:08

working4coins


People also ask

How do you interpolate a panda series?

These methods use the numerical values of the index. Both 'polynomial' and 'spline' require that you also specify an order (int), e.g. df. interpolate(method='polynomial', order=5). 'krogh', 'piecewise_polynomial', 'spline', 'pchip', 'akima': Wrappers around the SciPy interpolation methods of similar names.

How do pandas interpolate missing values?

You can interpolate missing values ( NaN ) in pandas. DataFrame and Series with interpolate() . This article describes the following contents. Use dropna() and fillna() to remove missing values NaN or to fill them with a specific value.

Which interpolation method is best for time series?

Linear interpolation is the most straightforward and commonly used interpolation method. It comes naturally when we have two points, we connect them with a straight line to fill out the missing information in between.

How do I use Fillna in pandas?

Pandas DataFrame fillna() MethodThe fillna() method replaces the NULL values with a specified value. The fillna() method returns a new DataFrame object unless the inplace parameter is set to True , in that case the fillna() method does the replacing in the original DataFrame instead.


1 Answers

Thank-you Dan Allan. I'm afraid I don't have the reputation to comment but Dan Allan's interpolate function raises an exception if asked to interpolate a time already defined in the ts index. E.g.

s = pd.to_datetime('2015-08-26 00:00:00')
e = pd.to_datetime('2015-08-26 00:10:00')
ts=pd.Series([0,8000],index=[s,e])
interpolate(ts,pd.to_datetime('2015-08-26 00:00:00'))

My minor modification of the above is:

def interpolate(ts, target):
    if target in ts.index:
        return ts[target]
    ts1 = ts.sort_index()
    b = (ts1.index > target).argmax() # index of first entry after target
    s = ts1.iloc[b-1:b+1]
    # Insert empty value at target time.
    s = s.reindex(pd.to_datetime(list(s.index.values) + [pd.to_datetime(target)]))
    return s.interpolate(method='time').loc[target]
like image 70
Jim O Avatar answered Sep 27 '22 18:09

Jim O