pandas读取混合date格式的excel

我正在尝试读取具有一列中的date值的Excel。 然而,它们有两种不同的格式:

03.07.2017 03.07.2017 30/06/2017 30/06/2017 30/06/2017 03.07.2017 03.07.2017 

07和06是月份数字。

我inputexcel:

 denik=pd.read_excel('obchodnidenik.xlsx',converters={'Vstup - datum':str}) 

但date转换不同。

我得到两个date格式切换date/月:

 '30/06/2017' '2017-03-07 00:00:00' 

将所有值转换为正确的date时间的最佳方法是什么?

你可以在读取excel之后使用Series.replace ,然后将其转换为适当的date时间,并设置dayfirst = True以获得正确的月份示例:

 n = ['03.07.2017','03.07.2017','30/06/2017','30/06/2017','30/06/2017','03.07.2017','03.07.2017'] df = pd.DataFrame(n) df[0]=df[0].replace('[/\/.]','-',regex=True) df[0] = pd.to_datetime(df[0],dayfirst=True) 

输出:

 0 2017-07-03
 1 2017-07-03
 2 2017-06-30
 3 2017-06-30
 4 2017-06-30
 5 2017-07-03
 6 2017-07-03