Pandas excel import changes the Date format

前端 未结 1 431
梦毁少年i
梦毁少年i 2021-01-25 21:45

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 for

相关标签:
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]:
       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
    

    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]:
       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
    
    0 讨论(0)
提交回复
热议问题