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
在哪裏數據框的休息嗎?爲什麼它只包含數據直到第一個有效值?
非常感謝幫助。非常感謝提前
有點晚,但遲到總比不到好:非常感謝很多這個,它幫助我解決了這個問題! – Blackbrook