How to do interpolation on datetime and float

陌路散爱 提交于 2019-12-07 11:59:24

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!