Python Numpy or Pandas Linear Interpolation For Datetime related Values

浪尽此生 提交于 2019-12-05 18:30:49

Using pandas git master (98e48ca) you can do the following:

In [27]: n = 4

In [28]: df = DataFrame(randn(n, 2), index=date_range('1/1/2001', periods=n, freq='30S'))

In [29]: resampled = df.resample('S')

In [30]: resampled.head()
Out[30]:
                         0      1
2001-01-01 00:00:00 -1.045 -1.067
2001-01-01 00:00:01    NaN    NaN
2001-01-01 00:00:02    NaN    NaN
2001-01-01 00:00:03    NaN    NaN
2001-01-01 00:00:04    NaN    NaN

[5 rows x 2 columns]

In [31]: interp = resampled.interpolate()

In [32]: interp.head()
Out[32]:
                         0      1
2001-01-01 00:00:00 -1.045 -1.067
2001-01-01 00:00:01 -1.014 -1.042
2001-01-01 00:00:02 -0.983 -1.018
2001-01-01 00:00:03 -0.952 -0.993
2001-01-01 00:00:04 -0.921 -0.969

[5 rows x 2 columns]

In [33]: interp.tail()
Out[33]:
                         0      1
2001-01-01 00:01:26  0.393  0.622
2001-01-01 00:01:27  0.337  0.571
2001-01-01 00:01:28  0.281  0.519
2001-01-01 00:01:29  0.225  0.468
2001-01-01 00:01:30  0.169  0.416

[5 rows x 2 columns]

By default Series.interpolate() performs linear interpolation. You can use DataFrame.resample() with irregularly sampled data as well.

Ok, I did this:

first = datetime(2013,12,8,0,0,0)
second = datetime(2013,12,8,0,2,0)
dates = [first,second]
x = np.array([617003.390723, 884235.38059])
newRange =  date_range(first, second, freq='S')
z = np.array([x[0]])
for i in range(1,len(newRange)-1):
    z = np.append(z,np.array([np.nan]))
z = np.append(z,np.array([1]))
print len(z)
print len(newRange)
ts = Series(z, index=newRange)
ts = ts.interpolate()
print ts.head()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!