pandasExcel导入更改date格式

我学习python(3.6与anaconda)为我的研究。

我使用pandas导入2列xls文件:date(dd-mm-yyyy)和价格。 但pandas改变date格式:

xls_file = pd.read_excel('myfile.xls') print(xls_file.iloc[0, 0]) 

我越来越 :

 2010-01-04 00:00:00 

代替 :

 04-01-2010 or at least : 2010-01-04 

我不知道为什么hh:mm:ss被添加,我得到相同的结果从date列的每一行。 我也尝试使用to_datetime不同的东西,但它没有修复它。

任何想法 ?

谢谢

你需要的是定义datetime值打印的格式。 可能有一个更优雅的方式来做到这一点,但这样的事情会起作用:

 In [11]: df Out[11]: id date 0 1 2017-09-12 1 2 2017-10-20 # Specifying the format In [16]: print(pd.datetime.strftime(df.iloc[0,1], "%Y-%m-%d")) 2017-09-12 

如果你想把date作为string存储在你的特定格式中,那么你也可以这样做:

 In [17]: df["datestr"] = pd.datetime.strftime(df.iloc[0,1], "%Y-%m-%d") In [18]: df Out[18]: id date datestr 0 1 2017-09-12 2017-09-12 1 2 2017-10-20 2017-09-12 In [19]: df.dtypes Out[19]: id int64 date datetime64[ns] datestr object dtype: object