Error parsing datetime string “09-11-2017 00:02:00” at position 8

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

I created a data frame with a column of datetime objects, re sampled it but would now like to turn the data frame into a list of lists - where the datetimes are now strings again.

for i in range(1, len(dataf.index)):     dataf["Time Stamp"][i] = datetime.strftime(dataf["Time Stamp"][i], '%m-%d-%Y %H:%M:%S')     print(dataf["Time Stamp"][i]) 

I keep getting the error (note the print part is just for me to check the output)

ValueError: Error parsing datetime string "09-11-2017 00:02:00" at position 8 

But from what I can tell my date format is exactly the same. I've even tried different capitalization in '%m-%d-%Y %H:%M:%S' to no avail.

Any idea?

回答1:

You ought to be able to

dataf['Time Stamp'].dt.strftime('%m-%d-%Y %H:%M:%S') 

So to rewrite the column

dataf['Time Stamp'] = dataf['Time Stamp'].dt.strftime('%m-%d-%Y %H:%M:%S') 

If you have errors, it's probably because the column isn't actually datetime.

dataf['Time Stamp'] = pd.to_datetime(     dataf['Time Stamp'] ).dt.strftime('%m-%d-%Y %H:%M:%S') 

If you have non-parsable data

dataf['Time Stamp'] = pd.to_datetime(     dataf['Time Stamp'], errors='coerce' ).dt.strftime('%m-%d-%Y %H:%M:%S') 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!