pandas -“时间数据与格式不匹配”​​;字符串与格式匹配时出错? [英] Pandas - "time data does not match format " error when the string does match the format?

查看:159
本文介绍了 pandas -“时间数据与格式不匹配”​​;字符串与格式匹配时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个值错误,说我的数据与格式不匹配。不知道这是一个错误还是我在这里丢失了一些东西。我是指本文档中的字符串格式。怪异的部分是,如果我将数据数据帧写入csv并读入,然后调用其下面的函数将转换日期,所以我不确定为什么不写入csv就无法正常工作。

I'm getting a value error saying my data does not match the format when it does. Not sure if this is a bug or I'm missing something here. I'm referring to this documentation for the string format. The weird part is if I write the 'data' Dataframe to a csv and read it in then call the function below it will convert the date so I'm not sure why it doesn't work without writing to a csv.

有什么想法吗?

data['Date'] = pd.to_datetime(data['Date'], format='%d-%b-%Y')  

我遇到两个错误

 TypeError: Unrecognized value type: <class 'str'>  

 ValueError: time data '27‑Aug‑2018' does not match format '%d-%b-%Y' (match) 

示例日期-

2‑Jul‑2018     
27‑Aug‑2018    
28‑May‑2018    
19‑Jun‑2017    
5‑Mar‑2018     
15‑Jan‑2018    
11‑Nov‑2013    
23‑Nov‑2015    
23‑Jun‑2014    
18‑Jun‑2018    
30‑Apr‑2018    
14‑May‑2018    
16‑Apr‑2018    
26‑Feb‑2018    
19‑Mar‑2018    
29‑Jun‑2015   

是不是因为它们都不都是两位数的日子?一位数字天的字符串格式值是多少?看起来这可能是原因,但是我不确定为什么它会在 27上出错。

Is it because they all aren't double digit days? What is the string format value for single digit days? Looks like this could be the cause but I'm not sure why it would error on the '27' though.

最终解决方案(它是unicode,而不是字符串)-

End solution (It was unicode & not a string) -

data['Date'] = data['Date'].apply(unidecode.unidecode)  
data['Date'] = data['Date'].apply(lambda x: x.replace("-", "/"))    
data['Date'] = pd.to_datetime(data['Date'], format="%d/%b/%Y")


推荐答案

日期字符串似乎有问题。我用示例数据复制了您的问题,如果我删除了连字符并手动替换了它们(对于前三个日期),则代码有效

There seems to be an issue with your date strings. I replicated your issue with your sample data and if I remove the hyphens and replace them manually (for the first three dates) then the code works

pd.to_datetime(df1['Date'] ,errors ='coerce')

输出:

0    2018-07-02
1    2018-08-27
2    2018-05-28
3           NaT
4           NaT
5           NaT
6           NaT
7           NaT
8           NaT
9           NaT
10          NaT
11          NaT
12          NaT
13          NaT
14          NaT
15          NaT

底线:您的连字符看起来像普通的连字符,但实际上又是别的东西,只需清理源数据即可,好了

Bottom line: your hyphens look like regular ones but are actually something else, just clean your source data and you're good to go

这篇关于 pandas -“时间数据与格式不匹配”​​;字符串与格式匹配时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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