2014-05-21 66 views
1

我有這樣一些數據:大熊貓重新取樣用了一整天的範圍

2013-10-01 11:11:22, 12 
2013-10-01 11:15:22, 12 
2013-10-01 15:11:22, 12 
2013-10-01 16:11:22, 12 
2013-10-01 16:45:22, 12 

我用下面的代碼重新取樣數據:

其結果將是這樣的:

2013-10-01 11:00:00, 2 
2013-10-01 12:00:00, 0 
2013-10-01 13:00:00, 0 
2013-10-01 14:00:00, 0 
2013-10-01 15:00:00, 1 
2013-10-01 16:00:00, 2 

但是我希望全天範圍從00:00:00到23:00:00,我如何更改resample日期範圍?

回答

3

那麼,您可以在重新採樣後使用小時期指數進行全天的reindex。所以像下面這樣:

>>> rng = pd.period_range('2013-10-01',periods=24, freq='H').to_timestamp() 
>>> df.resample('1H', how=lambda x: len(x)).reindex(index=rng,fill_value=0) 

        value 
2013-10-01 00:00:00  0 
2013-10-01 01:00:00  0 
2013-10-01 02:00:00  0 
2013-10-01 03:00:00  0 
2013-10-01 04:00:00  0 
2013-10-01 05:00:00  0 
2013-10-01 06:00:00  0 
2013-10-01 07:00:00  0 
2013-10-01 08:00:00  0 
2013-10-01 09:00:00  0 
2013-10-01 10:00:00  0 
2013-10-01 11:00:00  2 
2013-10-01 12:00:00  0 
2013-10-01 13:00:00  0 
2013-10-01 14:00:00  0 
2013-10-01 15:00:00  1 
2013-10-01 16:00:00  2 
2013-10-01 17:00:00  0 
2013-10-01 18:00:00  0 
2013-10-01 19:00:00  0 
2013-10-01 20:00:00  0 
2013-10-01 21:00:00  0 
2013-10-01 22:00:00  0 
2013-10-01 23:00:00  0 
+0

這就是我想要的,非常感謝。 – aisensiy

0

您應該先將您的日期轉換爲from datetime import datetime的日期時間格式,然後再使用pd.Series(datetime.strptime...)。檢查https://docs.python.org/2/library/datetime.html然後你可以更容易地操縱你的數據。

+0

這是DateTime或我不能重新採樣它。 – aisensiy