线性插值Pandas时间序列

时间:2016-10-20 17:46:07

标签: python pandas

我使用Pandas提取汇率数据。数据没有每一天的值。我想使用Pandas interoplate函数填写缺少的时间序列,以便所有日期都包含在索引中。例如,2010-01-09和2010-01-10都缺失了。 interoplate函数似乎没有做任何事情,但我无法弄清楚原因。

from pandas_datareader import data

can = data.get_data_fred('DEXCAUS')
can = can.interpolate(method='linear')
can = can.dropna()
print can.head(10)

输出:

            DEXCAUS
DATE               
2010-01-04   1.0377
2010-01-05   1.0371
2010-01-06   1.0333
2010-01-07   1.0351
2010-01-08   1.0345
2010-01-11   1.0317
2010-01-12   1.0374
2010-01-13   1.0319
2010-01-14   1.0260
2010-01-15   1.0287

期望的输出:

            DEXCAUS
DATE               
2010-01-04   1.0377
2010-01-05   1.0371
2010-01-06   1.0333
2010-01-07   1.0351
2010-01-08   1.0345
2010-01-09   some value..
2010-01-10   some value..
2010-01-11   1.0317
2010-01-12   1.0374
2010-01-13   1.0319
2010-01-14   1.0260
2010-01-15   1.0287

1 个答案:

答案 0 :(得分:2)

您需要先重新取样:

df.resample('D').interpolate(method='linear')
Out: 
             DEXCAUS
DATE                
2010-01-04  1.037700
2010-01-05  1.037100
2010-01-06  1.033300
2010-01-07  1.035100
2010-01-08  1.034500
2010-01-09  1.033567
2010-01-10  1.032633
2010-01-11  1.031700
2010-01-12  1.037400
2010-01-13  1.031900
2010-01-14  1.026000
2010-01-15  1.028700