Skip to content Skip to sidebar Skip to footer

Pandas Excel Import Changes The Date Format

Im learning python (3.6 with anaconda) for my studies. Im using pandas to import a xls file with 2 columns : Date (dd-mm-yyyy) and price. But pandas changes the date format : xls_

Solution 1:

What you need is to define the format that the datetime values get printed. There might be a more elegant way to do it but something like that will work:

In [11]: df
Out[11]:
   iddate
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

If you want to store the date as string in your specific format then you can also do something like:

In [17]: df["datestr"] = pd.datetime.strftime(df.iloc[0,1], "%Y-%m-%d")
In [18]: df
Out[18]:
   iddate     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

Post a Comment for "Pandas Excel Import Changes The Date Format"