Pandas - “time data does not match format ” error when the string does match the format?

前端 未结 2 2046
执念已碎
执念已碎 2021-01-15 21:28

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 documentat

相关标签:
2条回答
  • 2021-01-15 22:03

    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')
    

    output:

    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

    0 讨论(0)
  • 2021-01-15 22:21

    You got a special mark here it is not -

    df.iloc[0,0][2]
    Out[287]: '‑'
    

    Replace it with '-'

    pd.to_datetime(df.iloc[:,0].str.replace('‑','-'),format='%d-%b-%Y')
    Out[288]: 
    0    2018-08-27
    1    2018-05-28
    2    2017-06-19
    3    2018-03-05
    4    2018-01-15
    5    2013-11-11
    6    2015-11-23
    7    2014-06-23
    8    2018-06-18
    9    2018-04-30
    10   2018-05-14
    11   2018-04-16
    12   2018-02-26
    13   2018-03-19
    14   2015-06-29
    Name: 2‑Jul‑2018, dtype: datetime64[ns]
    
    0 讨论(0)
提交回复
热议问题