将不包含定界符的int64(YYYYMMDDHHMMSS)列转换为datetime64 [英] Convert a column of int64 (YYYYMMDDHHMMSS) into datetime64 without delimiters

查看:100
本文介绍了将不包含定界符的int64(YYYYMMDDHHMMSS)列转换为datetime64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的熊猫数据框中的数据.我想将 dateTime 列转换为 datetime64 ,以便我可以检查是否存在重复的 fileName ,然后找到具有最新 dateTime的文件

this is the data in my pandas dataframe. i want to convert the dateTime column into a datetime64 so i can check if duplicate fileName exist then find the file with the latest dateTime

如您所见,此处的日期时间格式为 YYYYMMDDHHMMSS ,不带分隔符

as you can see the datetime format here is YYYYMMDDHHMMSS without delimiters

                      filePath        dateTime fileName
0  Line20170601142525_line1.dat  20170601142525    line1
1  Line20170601142525_line2.dat  20170601142525    line2
2  Line20170601142526_line2.dat  20170601142526    line2
3  Line20170601142526_line3.dat  20170601142526    line3

Process finished with exit code 0

这是我的代码

formattedFileDirectory['dateTime'] = pandas.to_datetime(formattedFileDirectory['dateTime'], format='%Y%M%d%H%m%S')

我收到此错误

TypeError: 'int' object is unsliceable

如果我将该列设置为字符串,则会出现此错误

if i set the column as string i get this error

ValueError: unconverted data remains: 5

推荐答案

我相信您需要在调用之前将dateTime列从 int 类型转换为 string 类型 pd.to_datetime .

I believe that you need to convert the dateTime column from int type to string type before you call pd.to_datetime on it.

如果收到有关未转换的数据"的ValueError,通常会告诉您指定的字符串或格式错误.尝试将'%Y%M%d%H%m%S'更改为'%Y%m%d%H%M%S'.您可以在此处中找到格式字符串的文档(一直滚动到底部).

If you get a ValueError about "unconverted data", it will usually tell you that either the string or the format you specified is bad. Try changing '%Y%M%d%H%m%S' into '%Y%m%d%H%M%S'. You can find the documentation on format strings here (scroll all the way to the bottom).

这篇关于将不包含定界符的int64(YYYYMMDDHHMMSS)列转换为datetime64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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