日期时间毫秒至秒(以 pandas 为单位) [英] Datetime milliseconds to seconds in Pandas

查看:164
本文介绍了日期时间毫秒至秒(以 pandas 为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在pandas数据框中有一个日期时间列,其值如下:

Have a datetime column in pandas dataframe with values like these:

time
2018-04-11 22:18:30.122
2018-04-11 23:00:21.399

我想知道如何舍入这些值,摆脱毫秒并仅将日期,小时,分钟和00表示为秒,像这样:

I'm wondering how can I round these values, get rid of milliseconds and represent only date, hour, minutes and 00 as seconds like this:

time
2018-04-11 22:18:00
2018-04-11 23:00:00

推荐答案

使用

Use floor with T for minutes for set 0 seconds:

#if necessary
#df['time'] = pd.to_datetime(df['time'])
df['time'] = df['time'].dt.floor('T')
#alternative solution
#df['time'] = df['time'].dt.floor('Min')

print (df)
                 time
0 2018-04-11 22:18:00
1 2018-04-11 23:00:00

我要 round 30sec更改为下一个后的时间:

I want round values time after 30sec is changed to next one:

df['time'] = df['time'].dt.round('T')
print (df)
                 time
0 2018-04-11 22:19:00
1 2018-04-11 23:00:00

这篇关于日期时间毫秒至秒(以 pandas 为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆