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

前端 未结 2 2075
执念已碎
执念已碎 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

提交回复
热议问题