将时间序列数据分成一行时间间隔(PythonicWay)-每小时 [英] Split Time Series Data Into Time Intervals in one line (PythonicWay) - Hourly
问题描述
我有一个带有时间列的分钟数据.我想仅使用日期时间格式的hours
创建一个新列,例如format ='%Y-%m-%d %H:%M:%S'
.我知道在R中,我们可以使用类似
I have a minute data that has the time column. I want to create new column with just hours
with date time format, for example format ='%Y-%m-%d %H:%M:%S'
. I know in R, we can use something like,
value$hour<- cut(as.POSIXct(paste(value$time),
format="%Y-%m-%d %H:%M:%S"), breaks="hour")
执行此操作时,得到以下输出(我需要)
When I do this, I get the following output, (which i need)
time hour
2017-02-10 00:00:00 2017-02-10 00:00:00
2017-02-10 00:01:00 2017-02-10 00:00:00
2017-02-10 00:02:00 2017-02-10 00:00:00
2017-02-10 00:03:00 2017-02-10 00:00:00
....
2017-12-1 10:05:00 2017-12-01 10:00:00
2017-12-1 10:06:00 2017-12-01 10:00:00
我也意识到有很多关于dt.date
,dt.hour
等的讨论.我可以在python中像这样进行以下操作,
I am also aware that there are many threads that discusses about dt.date
, dt.hour
etc. I can do the following in python like this,
value['date'] = value['time'].dt.date
value['hour'] = value['time'].dt.hour
我是否可以在python中做任何与上面提到的R类似的方法? 任何想法将不胜感激.预先感谢!
Is there any way that I can do in python that is similar to R as mentioned above in one line? Any thoughts would be appreciated. Thanks in advance!
推荐答案
您需要如果需要转换为datetime
列time
,请添加 to_datetime
:
If need convert to datetime
column time
add to_datetime
:
df['hour'] = pd.to_datetime(df['time']).dt.floor('H')
print (df)
time hour
0 2017-02-10 00:00:00 2017-02-10 00:00:00
1 2017-02-10 00:01:00 2017-02-10 00:00:00
2 2017-02-10 00:02:00 2017-02-10 00:00:00
3 2017-02-10 00:03:00 2017-02-10 00:00:00
4 2017-12-1 10:05:00 2017-12-01 10:00:00
5 2017-12-1 10:06:00 2017-12-01 10:00:00
这篇关于将时间序列数据分成一行时间间隔(PythonicWay)-每小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!