[dt.round
](http://pandas.pydata.org/pandas-
docs/stable/generated/pandas.Series.dt.round.html)
这就是应该怎么做…使用
dt.round
df.assign(Date=df.Date.dt.round('H')) Date Num0 2011-01-01 00:00:00 0.5779571 2011-01-01 01:00:00 0.9957482 2011-01-01 02:00:00 0.8640133 2011-01-01 03:00:00 0.4687624 2011-01-01 04:00:00 0.866827老答案
一种方法是设置索引并使用
resample
df.set_index('Date').resample('H').last().reset_index() Date Num0 2011-01-01 00:00:00 0.5779571 2011-01-01 01:00:00 0.9957482 2011-01-01 02:00:00 0.8640133 2011-01-01 03:00:00 0.4687624 2011-01-01 04:00:00 0.866827另一种选择是剥离
date和
hour组件
df.assign( Date=pd.to_datetime(df.Date.dt.date) + pd.to_timedelta(df.Date.dt.hour, unit='H')) Date Num0 2011-01-01 00:00:00 0.5779571 2011-01-01 01:00:00 0.9957482 2011-01-01 02:00:00 0.8640133 2011-01-01 03:00:00 0.4687624 2011-01-01 04:00:00 0.866827



