如何对日期时间和浮点数进行插值 [英] How to do interpolation on datetime and float
问题描述
我正在使用 scipy 在时间序列上.我的x轴数据为日期时间格式,y轴为浮点型,如:
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
现在在计算坡度slope = (y_hi-y_lo) / (x_hi-x_lo)
时出现错误TypeError: unsupported operand type(s) for /: 'float' and 'datetime.timedelta'
,这是一个明显的错误.有人可以指出我正确的方向,如何处理吗?
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 ?
推荐答案
您的问题是,您试图将浮点数除以datetime.timedelta
对象,正如您所说的那样,该对象显然会引发TypeError.
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.
这篇关于如何对日期时间和浮点数进行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!