Convert date from excel file to pandas

2019-07-13 03:00发布

I'm importing excel file, where the 'Date' column has different ways of writing:

      Date
13/03/2017
13/03/2017
13/03/2017
13/03/2017
   10/3/17
   10/3/17
    9/3/17
    9/3/17
    9/3/17
    9/3/17

Importing to pandas:

df = pd.read_excel('data_excel.xls')
df.Date = pd.to_datetime(df.Date)

results in:

                     Date
               13/03/2017
64             13/03/2017
65             13/03/2017
66             13/03/2017
67    2017-10-03 00:00:00
68    2017-10-03 00:00:00
69    2017-09-03 00:00:00
70    2017-09-03 00:00:00
71    2017-09-03 00:00:00
72    2017-09-03 00:00:00

Which means, pandas did not parse properly date and time:

10/3/17 -> 2017-10-03

when I tried to specify the format:

df.Date = pd.to_datetime(df.Date, format='%d%m%Y')

got the error:

ValueError: time data u'13/03/2017' does not match format '%d%m%Y' (match)

Question:

How to import properly date and times from the excel file to pandas?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-13 03:29

New answer:

Actually pd.to_datetime has a dayfirst keyword argument that is useful here:

df.Date = pd.to_datetime(df.Date,dayfirst=True)

Result:

>>> df.Date
0   2017-03-13
1   2017-03-13
2   2017-03-13
3   2017-03-13
4   2017-03-10
5   2017-03-10
6   2017-03-09
7   2017-03-09
8   2017-03-09
9   2017-03-09
Name: Date, dtype: datetime64[ns]

Old answer:

Use the third-party module dateutil which can handle these kinds of variations. It has a dayfirst keyword argument that is useful here:

import dateutil

df = pd.read_excel('data_excel.xls')
df.Date = df.Date.apply(lambda x: dateutil.parser.parse(x,dayfirst=True))

Result:

>>> df.Date
0   2017-03-13
1   2017-03-13
2   2017-03-13
3   2017-03-13
4   2017-03-10
5   2017-03-10
6   2017-03-09
7   2017-03-09
8   2017-03-09
9   2017-03-09
Name: Date, dtype: datetime64[ns]
查看更多
登录 后发表回答