简体   繁体   English

在 Pandas 中,如何将字符串转换为以毫秒为单位的日期时间对象?

[英]In Pandas, how to convert a string to a datetime object with milliseconds?

I have a column of event's timestamp in my dataframe, looks like "2016-06-10 18:58:25:675", the last 3 digits are milliseconds, is there a efficient way to transform this column to a Pandas datatime type?我的数据框中有一列事件时间戳,看起来像“2016-06-10 18:58:25:675”,最后 3 位数字是毫秒,有没有一种有效的方法可以将此列转换为 Pandas 数据时间类型?

When I try:当我尝试:

pd.to_datetime('2016-06-10 18:57:35:317')

An error occurs saying "Unknown string format".出现错误,提示“未知字符串格式”。

Something like this should work这样的事情应该工作

pd.to_datetime('2016-06-10 18:57:35:317', format="%Y-%m-%d %H:%M:%S:%f")

You can look up datetime formats here您可以在此处查找日期时间格式

Normally there is a period / full-stop between the seconds and microseconds.通常在秒和微秒之间有一个句点/句号。

Since your data does not have that you can specify how your data is formatted:由于您的数据没有,您可以指定数据的格式:

pd.to_datetime('2016-06-10 18:57:35:317', format='%Y-%m-%d %H:%M:%S:%f')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM