2017-04-13 99 views
1

「秒」和「分鐘」:刪除鑑於像一個數據幀一熊貓數據幀列

import numpy as np 
import pandas as pd 

df = pd.DataFrame(
{'Date' : pd.date_range('1/1/2011', periods=5, freq='3675S'), 
'Num' : np.random.rand(5)}) 
       Date  Num 
0 2011-01-01 00:00:00 0.580997 
1 2011-01-01 01:01:15 0.407332 
2 2011-01-01 02:02:30 0.786035 
3 2011-01-01 03:03:45 0.821792 
4 2011-01-01 04:05:00 0.807869 

我想去掉「分鐘」和「秒」的信息。

以下(大多來自被盜:How to remove the 'seconds' of Pandas dataframe index?)工作好,

df = df.assign(Date = lambda x: pd.to_datetime(x['Date'].dt.strftime('%Y-%m-%d %H'))) 
       Date  Num 
0 2011-01-01 00:00:00 0.580997 
1 2011-01-01 01:00:00 0.407332 
2 2011-01-01 02:00:00 0.786035 
3 2011-01-01 03:00:00 0.821792 
4 2011-01-01 04:00:00 0.807869 

但感覺奇怪的日期時間轉換爲字符串,然後返回日期時間。有沒有辦法更直接地做到這一點?

回答

4

dt.round

這是它應該怎麼做......使用dt.round

df.assign(Date=df.Date.dt.round('H')) 

       Date  Num 
0 2011-01-01 00:00:00 0.577957 
1 2011-01-01 01:00:00 0.995748 
2 2011-01-01 02:00:00 0.864013 
3 2011-01-01 03:00:00 0.468762 
4 2011-01-01 04:00:00 0.866827 

OLD ANSWER

一種方法是設置索引和使用resample

df.set_index('Date').resample('H').last().reset_index() 

       Date  Num 
0 2011-01-01 00:00:00 0.577957 
1 2011-01-01 01:00:00 0.995748 
2 2011-01-01 02:00:00 0.864013 
3 2011-01-01 03:00:00 0.468762 
4 2011-01-01 04:00:00 0.866827 

另一種方法是剝離datehour組件

df.assign(
    Date=pd.to_datetime(df.Date.dt.date) + 
     pd.to_timedelta(df.Date.dt.hour, unit='H')) 

       Date  Num 
0 2011-01-01 00:00:00 0.577957 
1 2011-01-01 01:00:00 0.995748 
2 2011-01-01 02:00:00 0.864013 
3 2011-01-01 03:00:00 0.468762 
4 2011-01-01 04:00:00 0.866827 
+0

原來'我雖然想到'dt.round'是一般較好dt.floor'工作更好地爲我的情況。 -謝謝 –