Convert Excel style date with pandas

前端 未结 2 1284
盖世英雄少女心
盖世英雄少女心 2020-11-27 19:53

I have to parse an xml file which gives me datetimes in Excel style; for example: 42580.3333333333.

Does Pandas provide a way to convert that number int

2条回答
  •  借酒劲吻你
    2020-11-27 20:22

    You can use the 3rd party xlrd library before passing to pd.to_datetime:

    import xlrd
    
    def read_date(date):
        return xlrd.xldate.xldate_as_datetime(date, 0)
    
    df = pd.DataFrame({'date':[42580.3333333333, 10023]})
    
    df['new'] = pd.to_datetime(df['date'].apply(read_date), errors='coerce')
    
    print(df)
    
               date                 new
    0  42580.333333 2016-07-29 08:00:00
    1  10023.000000 1927-06-10 00:00:00
    

提交回复
热议问题