2015-05-05 46 views
4

嗨我想插入一個數據幀,我有一個日期時間索引索引。插值並用日期時間索引填充熊貓數據框

這裏的數據

res = pd.DataFrame(cursor.execute("SELECT DATETIME,VALUE FROM {} WHERE DATETIME > ? AND DATETIME < ?".format(table),[start,end]).fetchall(),columns=['date','value']) 
res.set_index('date',inplace=True) 

產生

2013-01-31 00:00:00 517 
2012-12-31 00:00:00 263 
2012-11-30 00:00:00 1917 
2012-10-31 00:00:00 391 
2012-09-30 00:00:00 782 
2012-08-31 00:00:00 700 
2012-07-31 00:00:00 799 
2012-06-30 00:00:00 914 
2012-05-31 00:00:00 141 
2012-04-30 00:00:00 342 
2012-03-31 00:00:00 199 
2012-02-29 00:00:00 533 
2012-01-31 00:00:00 1393 
2011-12-31 00:00:00 497 
2011-11-30 00:00:00 1457 
2011-10-31 00:00:00 997 
2011-09-30 00:00:00 533 
2011-08-31 00:00:00 626 
2011-07-31 00:00:00 1933 
2011-06-30 00:00:00 4248 
2011-05-31 00:00:00 1248 
2011-04-30 00:00:00 904 
2011-03-31 00:00:00 3280 
2011-02-28 00:00:00 390 
2011-01-31 00:00:00 601 
2010-12-31 00:00:00 423 
2010-11-30 00:00:00 748 
2010-10-31 00:00:00 433 
2010-09-30 00:00:00 734 
2010-08-31 00:00:00 845 
2010-07-31 00:00:00 1693 
2010-06-30 00:00:00 2742 
2010-05-31 00:00:00 669 

這是所有的非連續的。我想每天都有一個價值,所以想用某種內插來填補缺失的值。

首先嚐試設置索引,然後進行插值。

new_index = pd.date_range(date(2010,1,1),date(2014,1,31),freq='D') 
df2 = res.reindex(new_index) # This returns NaN 
df2.interpolate('cubic') # Fails with error TypeError: Cannot interpolate with all NaNs. 

我會希望得到的回覆是與2010 - 2014年之間的每個日期值,與周圍的點計算的插值一個數據幀。

似乎有可能簡單地做到這一點,但我不知道是什麼。

+0

下面做工精細的答案,所以做我的原創,除了一件事。 dtypes有所作爲。將dtype ='float32'添加到初始Dataframe構造中,並將索引類型設置爲DatetimeIndex,以確保建議的解決方案都在下面工作 – Ronnie

回答

3

下面介紹一種方法。

首先從df.indexmax min日期

In [152]: df_reindexed = df.reindex(pd.date_range(start=df.index.min(), 
                end=df.index.max(), 
                freq='1D'))     

得到一個新的索引,然後使用interpolate(method='linear')的系列得到的值。

In [153]: df_reindexed.interpolate(method='linear')                  
Out[153]:                             
        Value                         
2010-05-31 669.000000                         
2010-06-01 738.100000                         
2010-06-02 807.200000                         
2010-06-03 876.300000                         
2010-06-04 945.400000                         
2010-06-05 1014.500000                         
...                         
2013-01-25 467.838710                         
2013-01-26 476.032258                         
2013-01-27 484.225806                         
2013-01-28 492.419355                         
2013-01-29 500.612903                         
2013-01-30 508.806452                         
2013-01-31 517.000000                         

[977 rows x 1 columns]                         
+0

似乎沒有任何不同之處。 df.reindex調用返回一列NaN,隨後內插失敗。 使用0.15.2如果有幫助 – Ronnie

+0

您是否看到上述解決方案中的「2012-12-31」和「2013-01-31」之間的插值,這些插值不在您的原始數據中?另外,爲了確保我們使用相同類型的數據,在原始數據中,「df.index」包含您的日期。 – Zero

+0

Nope > df.index給出指數([u'2015-01-31 00:00:00',2014'u14-12-31 00:00:00',2014年u11-11-30 00:00'') :00',... – Ronnie

3

,正如添加到@ JohnGalt的回答,您還可以使用resamplereindex這裏稍微更方便:

df.resample('D').interpolate('cubic') 

        value 
date     
2010-05-31 669.000000 
2010-06-01 830.400272 
2010-06-02 983.988431 
2010-06-03 1129.919466 
2010-06-04 1268.348368 
2010-06-05 1399.430127 
2010-06-06 1523.319734 

... 

2010-06-25 2716.850752 
2010-06-26 2729.445324 
2010-06-27 2738.102544 
2010-06-28 2742.977403 
2010-06-29 2744.224892 
2010-06-30 2742.000000 
2010-07-01 2736.454249 
2010-07-02 2727.725284 
2010-07-03 2715.947277 
相關問題