在pandas中使用to_datetime时出错

时间:2017-11-18 13:16:33

标签: python pandas datetime

我有一个这样的数据框:

         Date  Quote-Spread
0  2013-11-17      0.010000
1  2013-12-10      0.020000
2  2013-12-11      0.013333
3  2014-06-01      0.050000
4  2014-06-23      0.050000

当我使用此代码时,我引发错误:

import pandas as pd
pd.to_datetime(df1['Date'] ,format ="%Y%m%d")

ValueError: time data '2013-11-17' does not match format '%Y%m%d' (match)

我该如何更正此错误?

2 个答案:

答案 0 :(得分:4)

仅使用to_datetime

df1['Date'] = pd.to_datetime(df1['Date'])
print (df1['Date'])
0   2013-11-17
1   2013-12-10
2   2013-12-11
3   2014-06-01
4   2014-06-23
Name: Date, dtype: datetime64[ns]

或者,如果要指定格式添加-,因为%Y%m%d匹配YYMMDD且您的格式为YY-MM-DD

pd.to_datetime(df1['Date'], format ="%Y-%m-%d")

答案 1 :(得分:2)

to_datetime是要走的路。与使用列表理解或应用的替代方法相比,它也是最快的。

import pandas as pd
import datetime

# Create dataset
df1 = pd.DataFrame(dict(Date=['2013-11-17','2013-12-10']*10000))

Alt1,列表理解:

df1.Date = [datetime.datetime.strptime(i,"%Y-%m-%d") for i in df1.Date.values]

Alt2,申请:

df1.Date = df1.Date.apply(lambda x: datetime.datetime.strptime(x,"%Y-%m-%d"))

Alt3,to_datetime:

df1.Date = pd.to_datetime(df1.Date)

<强>计时

1 loop, best of 3: 744 ms per loop #1
1 loop, best of 3: 793 ms per loop #2 
100 loops, best of 3: 18.5 ms per loop #3