解析日期时间字符串"09-11-2017 00:02:00"时出错在位置8 [英] Error parsing datetime string "09-11-2017 00:02:00" at position 8

查看:63
本文介绍了解析日期时间字符串"09-11-2017 00:02:00"时出错在位置8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有一列日期时间对象的数据框,对其进行了重新采样,但现在想将该数据框变成一个列表列表-日期时间现在又是字符串.

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

但是据我所知,日期格式是完全相同的.我什至尝试在'%m-%d-%Y%H:%M:%S'中使用不同的大小写,但无济于事.

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.

有什么主意吗?

推荐答案

您应该能够

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

所以要重写列

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

这篇关于解析日期时间字符串"09-11-2017 00:02:00"时出错在位置8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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