【发布时间】:2016-07-14 20:25:42
【问题描述】:
考虑以下代码:
import pandas as pd
some_time='01/01/2011 12:02:41 AM'
print(pd.to_datetime(some_time))
print(pd.to_datetime(some_time, format='%m/%d/%Y %I:%M:%S %r'))
第一个to_datetime() 转换工作并打印输出
2011-01-01 00:02:41
不幸的是,在我的实际应用程序中,我正在处理一个包含超过 200 万行的 DataFrame,并且默认的 to_datetime() 速度非常慢,即使我在关键字参数中设置了 infer_datetime_format=True。
我读到to_datetime() 可以通过明确指定字符串格式来加速。我在http://www.tutorialspoint.com/python/time_strftime.htm 之后尝试过这个,但是我上面的尝试失败并出现错误ValueError: 'r' is a bad directive in format '%m/%d/%Y %I:%M:%S %r'。
如何指定正确的 strftime 格式以将'01/01/2011 12:02:41 AM' 转换为日期时间?
【问题讨论】: