将时间序列数据拆分为一行中的时间间隔(PythonicWay)-每小时
我有一个包含时间列的分钟数据。我想用日期-时间格式创建一个新的列,该列只有将时间序列数据拆分为一行中的时间间隔(PythonicWay)-每小时,python,pandas,Python,Pandas,我有一个包含时间列的分钟数据。我想用日期-时间格式创建一个新的列,该列只有hours,例如format='%Y-%m-%d%H:%m:%S'。我知道在R里,我们可以用 value$hour<- cut(as.POSIXct(paste(value$time), format="%Y-%m-%d %H:%M:%S"), breaks="hour") 我还知道有很多线程讨论了dt.date,dt.hour等。我可以用python这样做: v
hours
,例如format='%Y-%m-%d%H:%m:%S'
。我知道在R里,我们可以用
value$hour<- cut(as.POSIXct(paste(value$time),
format="%Y-%m-%d %H:%M:%S"), breaks="hour")
我还知道有很多线程讨论了dt.date
,dt.hour
等。我可以用python这样做:
value['date'] = value['time'].dt.date
value['hour'] = value['time'].dt.hour
我能用python做什么类似于上面一行中提到的R的事情吗?
如有任何想法,将不胜感激。提前谢谢 您需要:
如果需要转换为datetime
列time
添加:
谢谢你,因为你有
df['time'].dt.floor('H')
完成了这个技巧,因为我已经将df['time']
转换为pd.to\u datetime
。但是,我的新列hour
仅显示日期和缺少的00:00:00
部分。想知道为什么吗?是的,在熊猫中,如果所有值都是00:00:00
,则不会显示。但是如果通过print(df.loc[0,'time'])检查值
它也会返回丢失的零。完美,你的意思是print(df.loc[0,'hour'])
对吗?这很好用,太棒了,谢谢你!我必须把旗子放在它改变的地方,我怎么做?
value['date'] = value['time'].dt.date
value['hour'] = value['time'].dt.hour
df['hour'] = 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-01 10:05:00 2017-12-01 10:00:00
5 2017-12-01 10:06:00 2017-12-01 10:00:00
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