Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

python - Can I parse dates in different formats?

A collaborator of mine has inconsistent date formatting in their data.

0   13/11/2016
1   21/01/2017
2   22/01/2017
3   2017-02-02
4   2016-12-11
5   13/11/2016
6   2016-12-12
7   21/01/2017
8   22/01/2017
9   2017-02-02

I'd like to be able to parse this into a timestamp for analysis in python. Using parse_dates = True doesn't work, and I suspect it is because of the mixture of formats.

Can I parse dates in different formats?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use to_datetime:

First format (YYYY-MM-DD):

print (df)
        dates
0  13/11/2016
1  21/01/2017
2  22/01/2017
3  2017-02-02
4  2016-12-11
5  13/11/2016
6  2016-12-12
7  21/01/2017
8  22/01/2017
9  2017-02-02
9  2017-02-25 <- YYYY-MM-DD

dates = pd.to_datetime(df.dates)
print (dates)
0   2016-11-13
1   2017-01-21
2   2017-01-22
3   2017-02-02
4   2016-12-11
5   2016-11-13
6   2016-12-12
7   2017-01-21
8   2017-01-22
9   2017-02-02
9   2017-02-25
Name: dates, dtype: datetime64[ns]

Second format (YYYY-DD-MM)

It is a bit problematic - need parameter format and errors='coerce' in to_datetime, last combine_first or fillna:

print (df)
        dates
0  13/11/2016
1  21/01/2017
2  22/01/2017
3  2017-02-02
4  2016-12-11
5  13/11/2016
6  2016-12-12
7  21/01/2017
8  22/01/2017
9  2017-02-02
9  2017-25-02 <- YYYY-DD-MM

dates1 = pd.to_datetime(df.dates, format='%d/%m/%Y', errors='coerce')
dates2 = pd.to_datetime(df.dates, format='%Y-%d-%m', errors='coerce')

dates = dates1.combine_first(dates2)
#dates = dates1.fillna(dates2)
print (dates)
0   2016-11-13
1   2017-01-21
2   2017-01-22
3   2017-02-02
4   2016-11-12
5   2016-11-13
6   2016-12-12
7   2017-01-21
8   2017-01-22
9   2017-02-02
9   2017-02-25
Name: dates, dtype: datetime64[ns]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...