日期以字符串列表形式给出,需要使用正则表达式来匹配不同的日期格式。最好在python
dates = ["04/20/2009", "04/20/09", "4/20/09","4/3/09","6/2008","12/2009","2009","2010"]
df = pd.Series(dates)
答案 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]