当它不在Excel列中时,如何从Pandas df列中删除时间?

我已经将两个工作表分别读入了pandas数据框。 两者都有date发布的列。

在这两个工作表中,该列以exce / dd / mm / yyyy保存。

WS1

13/02/2017 01/02/2017 08/11/2016 05/08/2016 16/03/2017 53 2017-02-13 51 2017-02-01 22 2016-11-08 0 2016-08-05 63 2017-03-16 Name: Date Issued, dtype: datetime64[ns] 

但是ws2

 08/03/2017 24/08/2016 28/11/2016 26/10/2016 10/03/2017 0 2017-03-08 00:00:00 1 2016-08-24 00:00:00 2 2016-11-28 00:00:00 3 2016-10-26 00:00:00 4 2017-03-10 00:00:00 Name: Date Issued, dtype: object 

为什么这些dtypes是不同的,我怎么能删除时间?

目前的代码看起来没有什么比普通的处理大pandas

 df = pd.read_excel(file, 'ws2') df = df.loc[:, ['Date Issued', 'Person ID', 'First Name', 'Surname', 'Type', 'Amount']] df = df.sort_values(by=['Surname']) df['Date Issued'] = pd.to_datetime(df_loan['Date Issued'], dayfirst=True) 

我曾尝试过使用;

 df['Date Issued'] = pd.to_datetime(df['Date Issued'], dayfirst=True) 

但是得到以下错误;

 TypeError: invalid string coercion to datetime 

也;

 df['Date Issued'] = df['Date Issued'].astype('datetime64[ns]') 

但是得到这个错误

 ValueError: Error parsing datetime string " " at position 1 

看起来至less有一个非date时间值。

所以需要参数errors='coerce'来将这些值NaTto_datetime NaT (date时间的NaN ):

 df['Date Issued'] = pd.to_datetime(df['Date Issued'], dayfirst=True, errors='coerce')