在 Pandas 中,如何将字符串转换为以毫秒为单位的日期时间对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38110263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Pandas, how to convert a string to a datetime object with milliseconds?
提问by bananana
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".
出现错误提示“未知字符串格式”。
回答by mgilbert
回答by mechanical_meat
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')