Python中使用Numpy或Pandas进行日期时间相关值的线性插值

4
我有以下数据,但我可以控制它的格式。基本上,我想使用Python和Numpy或Pandas来对数据进行插值,以实现每秒钟的二次插值,并使其分辨率更高。
所以我希望在保留原始值的同时,在我当前拥有的每个真实值之间线性插值并产生新值。
如何使用Pandas或Numpy完成这个任务?
以这种类型的数据为例:
       TIME               ECI_X            ECI_Y          ECI_Z
 2013-12-07 00:00:00, -7346664.77912, -13323447.6311, 21734849.5263,@
 2013-12-07 00:01:00, -7245621.40363, -13377562.35, 21735850.3527,@
 2013-12-07 00:01:30, -7142326.20854, -13432541.9267, 21736462.4521,@
 2013-12-07 00:02:00, -7038893.48454, -13487262.8599, 21736650.3293,@
 2013-12-07 00:02:30, -6935325.24526, -13541724.0946, 21736413.9937,@
 2013-12-07 00:03:00, -6833738.23865, -13594806.9333, 21735778.2218,@
 2013-12-07 00:03:30, -6729905.37597, -13648746.6281, 21734705.6406,@
 2013-12-07 00:04:00, -6625943.01291, -13702423.5112, 21733208.9233,@
 2013-12-07 00:04:30, -6521853.17291, -13755836.5481, 21731288.1125,@
 2013-12-07 00:05:00, -6419753.85176, -13807871.3011, 21729016.1386,@
 2013-12-07 00:05:30, -6315415.32918, -13860754.6497, 21726259.4135,@
 2013-12-07 00:06:00, -6210955.33186, -13913371.1187, 21723078.7695,@
 ...

我希望你能将这个功能实现,即每秒更新一次。

 2013-12-07 00:00:00, -7346664.77912, -13323447.6311, 21734849.5263,@
 2013-12-07 00:00:01, -7346665.10000, -13323448.1000, 21734850.1000,@
 ...
 2013-12-07 00:00:59, -7346611.10000, -13323461.1000, 21734850.1000,@
 2013-12-07 00:01:00, -7245621.40363, -13377562.3500, 21735850.3527,@

请给我一个实现这个功能的例子,谢谢!
我已经尝试过以下方法:
#! /usr/bin/python

import datetime
from pandas import *

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')
ts = Series(x, index=dates)
ts.interpolate()
print ts.head()

#2013-12-08 00:00:00, 617003.390723, -26471116.2566, 3974868.93334,@
#2013-12-08 00:02:00, 884235.38059, -26519366.9219, 3601627.52947,@

我该如何使用"newRange"在"x"的真实值之间创建线性插值呢?


2
看一下这个方法,当pandas 0.13版本即将发布时,它将得到大幅升级... - Dan Allan
http://pandas.pydata.org/pandas-docs/dev/generated/pandas.Series.interpolate.html?highlight=interpolate#pandas.Series.interpolate 在0.13中的文档。 - Jeff
2个回答

6

使用 pandas git 主分支 (98e48ca) 可以进行以下操作:

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]

默认情况下,Series.interpolate() 进行线性插值。你也可以使用 DataFrame.resample() 处理不规则采样的数据。


0

好的,我做了这个:

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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接