匹配不同日历日期格式日期“ M {0,1,2} / D {1,2} / Y {2,4}”,“ Y {4}”的正则表达式

时间:2019-04-17 19:35:42

标签: python regex pandas

日期以字符串列表形式给出,需要使用正则表达式来匹配不同的日期格式。最好在python

dates = ["04/20/2009", "04/20/09", "4/20/09","4/3/09","6/2008","12/2009","2009","2010"]
df = pd.Series(dates)

1 个答案:

答案 0 :(得分:2)

您不需要正则表达式。指定数据类型:

import pandas as pd
dates = ["04/20/2009", "04/20/09", "4/20/09","4/3/09","6/2008","12/2009","2009","2010"]
df = pd.Series(dates,dtype='datetime64[ns]')
print(df)

输出:

0   2009-04-20
1   2009-04-20
2   2009-04-20
3   2009-04-03
4   2008-06-01
5   2009-12-01
6   2009-01-01
7   2010-01-01
dtype: datetime64[ns]