Leave dates as strings using read_excel function from pandas in python

后端 未结 3 883
南旧
南旧 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 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)
    

提交回复
热议问题