Shivam Agrawal
Shivam Agrawal

Reputation: 2103

How to do interpolation on datetime and float

I am doing 1d interpolation using scipy on time-series. My x-axis data is in datetime format and y axis is in float like:

3/15/2012 16:00:00  32.94
3/16/2012 16:00:00  32.95
3/19/2012 16:00:00  32.61

Now during slope calculation slope = (y_hi-y_lo) / (x_hi-x_lo) i am getting the error TypeError: unsupported operand type(s) for /: 'float' and 'datetime.timedelta' which is an obvious error. Can someone point me toward the right direction, How to handle it ?

Upvotes: 1

Views: 7742

Answers (2)

reptilicus
reptilicus

Reputation: 10397

If you are working with timeseries data, the Pandas package is an excellent option. Here's an example of upsampling daily data to hourly data via interpolation:

import numpy as np
from pandas import *
rng = date_range('1/1/2011', periods=12, freq='D')
ts = Series(np.arange(len(rng)), index=rng)
resampled = ts.resample('H')
interp = resampled.interpolate()
In [5]: ts
Out[5]:
2011-01-01     0
2011-01-02     1
2011-01-03     2
2011-01-04     3
2011-01-05     4
2011-01-06     5
2011-01-07     6
2011-01-08     7
2011-01-09     8
2011-01-10     9
2011-01-11    10
2011-01-12    11

In [12]: interp.head()
Out[12]:
2011-01-01 00:00:00    0.000000
2011-01-01 01:00:00    0.041667
2011-01-01 02:00:00    0.083333
2011-01-01 03:00:00    0.125000
2011-01-01 04:00:00    0.166667
Freq: H, dtype: float64

In [13]: interp.tail()
Out[13]:
2011-01-11 20:00:00    10.833333
2011-01-11 21:00:00    10.875000
2011-01-11 22:00:00    10.916667
2011-01-11 23:00:00    10.958333
2011-01-12 00:00:00    11.000000
Freq: H, dtype: float64

Upvotes: 3

Ffisegydd
Ffisegydd

Reputation: 53688

Your issue is that you are trying to divide a float by a datetime.timedelta object which is, as you said, obviously throwing a TypeError.

You can convert datetime.timedelta objects to a float representing the total number of seconds within that timedelta using the datetime.timedelta.total_seconds() instance method.

In that case you would modify your code to something like:

slope_numerator = y_hi - y_lo
slope_denominator = (x_hi - x_lo).total_seconds()
slope = slope_numerator / slope_denominator

Note that this will give you a slope in terms of seconds. You could modify the denominator to give it in terms of hours, days, etc to suit your purposes.

Upvotes: 6

Related Questions