2015-06-08 56 views
2

我已經得到了以下數據的時間戳和NaN的填充:加入開始和結束日期擴展數據幀,並與

     data 
timestamp 
2012-06-01 17:00:00  9 
2012-06-01 20:00:00  8 
2012-06-01 13:00:00  9 
2012-06-01 10:00:00  9 

,並想對它進行排序降序按時間,在上面添加一個開始和結束日期和數據的底部,使得其看起來像這樣:

     data 
timestamp 
2012-06-01 00:00:00  NaN 
2012-06-01 10:00:00  9 
2012-06-01 13:00:00  9 
2012-06-01 17:00:00  9 
2012-06-01 20:00:00  8 
2012-06-02 00:00:00  NaN 

最後我想延長數據集,以覆蓋從開始所有小時在一層小時的步驟,結束,用含有「丟失的時間戳填充所述數據幀沒有'/'NaN'作爲數據。 到目前爲止,我有以下代碼:

df2 = pd.DataFrame({'data':temperature, 'timestamp': pd.DatetimeIndex(timestamp)}, dtype=float) 
df2.set_index('timestamp',inplace=True) 
df3 = pd.DataFrame({ 'timestamp': pd.Series([ts1, ts2]), 'data': [None, None]}) 
df3.set_index('timestamp',inplace=True) 
print(df3) 
merged = df3.append(df2) 
print(merged) 

具有以下的打印輸出:

df3: 
        data 
timestamp     
2012-06-01 00:00:00  None 
2012-06-02 00:00:00  None 


merged: 
        data 
timestamp     
2012-06-01 00:00:00  NaN 
2012-06-02 00:00:00  NaN 
2012-06-01 17:00:00  9 
2012-06-01 20:00:00  8 
2012-06-01 13:00:00  9 
2012-06-01 10:00:00  9 

我曾嘗試:

merged = merged.asfreq('H') 

但這返回一個不令人滿意的結果:

     data 
2012-06-01 00:00:00 NaN 
2012-06-01 01:00:00 NaN 
2012-06-01 02:00:00 NaN 
2012-06-01 03:00:00 NaN 
2012-06-01 04:00:00 NaN 
2012-06-01 05:00:00 NaN 
2012-06-01 06:00:00 NaN 
2012-06-01 07:00:00 NaN 
2012-06-01 08:00:00 NaN 
2012-06-01 09:00:00 NaN 
2012-06-01 10:00:00  9 

在哪裏數據框的休息嗎?爲什麼它只包含數據直到第一個有效值?

非常感謝幫助。非常感謝提前

回答

1

首先創建一個你想要的時間戳指數的空數據框中,然後做了左與原始數據集合並:

df2 = pd.DataFrame(index = pd.date_range('2012-06-01','2012-06-02', freq='H')) 
df3 = pd.merge(df2, df, left_index = True, right_index = True, how = 'left') 
df3 
Out[103]: 
           timestamp value 
2012-06-01 00:00:00     NaN NaN 
2012-06-01 01:00:00     NaN NaN 
2012-06-01 02:00:00     NaN NaN 
2012-06-01 03:00:00     NaN NaN 
2012-06-01 04:00:00     NaN NaN 
2012-06-01 05:00:00     NaN NaN 
2012-06-01 06:00:00     NaN NaN 
2012-06-01 07:00:00     NaN NaN 
2012-06-01 08:00:00     NaN NaN 
2012-06-01 09:00:00     NaN NaN 
2012-06-01 10:00:00 2012-06-01 10:00:00  9 
2012-06-01 11:00:00     NaN NaN 
2012-06-01 12:00:00     NaN NaN 
2012-06-01 13:00:00 2012-06-01 13:00:00  9 
2012-06-01 14:00:00     NaN NaN 
2012-06-01 15:00:00     NaN NaN 
2012-06-01 16:00:00     NaN NaN 
2012-06-01 17:00:00 2012-06-01 17:00:00  9 
2012-06-01 18:00:00     NaN NaN 
2012-06-01 19:00:00     NaN NaN 
2012-06-01 20:00:00 2012-06-01 20:00:00  8 
2012-06-01 21:00:00     NaN NaN 
2012-06-01 22:00:00     NaN NaN 
2012-06-01 23:00:00     NaN NaN 
2012-06-02 00:00:00     NaN NaN 
+0

有點晚,但遲到總比不到好:非常感謝很多這個,它幫助我解決了這個問題! – Blackbrook

相關問題