Leave dates as strings using read_excel function from pandas in python

后端 未结 3 874
南旧
南旧 2020-12-02 00:17

Python 2.7.10
Tried pandas 0.17.1 -- function read_excel
Tried pyexcel 0.1.7 + pyexcel-xlsx 0.0.7 -- function get_records()

相关标签:
3条回答
  • 2020-12-02 00:49

    I tried saving the file in a CSV UTF-8 format (manually) and used pd.read_csv() and worked fine.

    I tried a bunch of things to figure the same thing with read_excel. Did not work anything for me. So, I am guessing read_excel is probably updating your string in a datetime object which you can not control.

    0 讨论(0)
  • 2020-12-02 01:07
    • Using converters{'Date': str} option inside the pandas.read_excel which helps. pandas.read_excel(xlsx, sheet, converters={'Date': str})
    • you can try convert your timestamp back to the original format
      df['Date'][0].strftime('%Y/%m/%d')
    0 讨论(0)
  • 2020-12-02 01:09

    I ran into an identical problem, except pandas was oddly converting only some cells into datetimes. I ended up manually converting each cell into a string like so:

    def undate(x):
        if pd.isnull(x):
            return x
        try:
            return x.strftime('%d/%m/%Y')
        except AttributeError:
            return x
        except Exception:
            raise
    
    for i in list_of_possible_date_columns:
        df[i] = df[i].apply(undate)
    
    0 讨论(0)
提交回复
热议问题