擅长:python、mysql、java
<p><a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.interp.html" rel="nofollow noreferrer">^{<cd1>} function</a>期望<code>arr1</code>和<code>arr2</code>是一维浮点数序列,即,如果要使用<code>np.interp()</code>,应该将<code>datetime</code>对象的序列转换为一维浮点数序列。</p>
<p>如果输入数据对所有datetime对象使用相同的UTC偏移量,则可以通过从所有值中减去引用日期来获得浮点值。如果<a href="https://stackoverflow.com/questions/27926669/how-do-you-interpolate-from-an-array-containing-datetime-objects?noredirect=1#comment44274845_27926669">your input is UTC</a>(偏移量始终为零),则为真:</p>
<pre><code>from datetime import datetime
import numpy as np
arr1 = np.array([datetime(2008, 1, d) for d in range(1, 10)])
arr2 = np.arange(1, 10)
def to_float(d, epoch=arr1[0]):
return (d - epoch).total_seconds()
f = np.interp(to_float(datetime(2008,1,5,12)), map(to_float, arr1), arr2)
print f # -> 5.5
</code></pre>