简体   繁体   English

Pandas to_datetime 错误“未转换的数据仍然存在”

[英]Pandas to_datetime error 'unconverted data remains'

I'm trying to convert date column in my Pandas DataFrame to datetime format.我正在尝试将 Pandas DataFrame 中的日期列转换为日期时间格式。 If I don't specify date format, it works fine, but then further along in the code I get issues because of different time formats.如果我不指定日期格式,它可以正常工作,但是由于时间格式不同,在代码中我会遇到问题。

The original dates looks like this 10/10/2019 6:00 in european date format.欧洲日期格式的原始日期如下所示10/10/2019 6:00

I tried specifying format like so:我尝试像这样指定格式:

df['PeriodStartDate'] = pd.to_datetime(df['PeriodStartDate'], 
          format="%d/%m/%Y")

which results in an error: unconverted data remains 6:00这会导致错误: unconverted data remains 6:00

I then tried to update format directive to format="%d/%m/%Y %-I/%H" which comes up with another error: '-' is a bad directive in format '%d/%m/%Y %-I/%H' even though I thought that to_datetime uses the same directives and strftime and in the latter %-I is allowed.然后我尝试将格式指令更新为format="%d/%m/%Y %-I/%H"出现另一个错误: '-' is a bad directive in format '%d/%m/%Y %-I/%H'即使我认为to_datetime使用相同的指令和strftime并且在后者中%-I是允许的。

In frustration I then decided to chop off the end of the string since I don't really need hours and minutes:然后我沮丧地决定切断字符串的末端,因为我真的不需要小时和分钟:

    df['PeriodStartDate'] = df['PeriodStartDate'].str[:10]
    df['PeriodStartDate'] = pd.to_datetime(df['PeriodStartDate'], 
          format="%d/%m/%Y")

But this once again results in an error: ValueError: unconverted data remains: which of course comes from the fact that some dates have 9 digits like 3/10/2019 6:00但这再次导致错误: ValueError: unconverted data remains:这当然来自某些日期有 9 位数字的事实,例如3/10/2019 6:00

Not quite sure where to go from here.不太清楚 go 从这里到哪里。

format %H:%M would work( don't forget the : in between )格式%H:%M会起作用(不要忘记中间的:

pd.to_datetime('10/10/2019 6:00', format="%m/%d/%Y %H:%M")

Out[1049]: Timestamp('2019-10-10 06:00:00')

pd.to_datetime('3/10/2019 18:00', format="%d/%m/%Y %H:%M")

Out[1064]: Timestamp('2019-10-03 18:00:00')

Oh, I feel so dumb.哦,我觉得自己好笨。 I figured out what the issue was.我弄清楚了问题所在。 For some reason I thought that hours were in a 12-hour format, but they were in fact in a 24-hour format, so changing directive to "%d/%m/%Y %H:%M" solved it.出于某种原因,我认为小时是 12 小时格式,但实际上它们是 24 小时格式,因此将指令更改为"%d/%m/%Y %H:%M"解决了它。

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

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