formatting timedelta64 when using pandas.to_excel

前端 未结 2 511
春和景丽
春和景丽 2021-01-13 07:08

I am writing to an excel file using an ExcelWriter:

writer = pd.ExcelWriter(fn,datetime_format=\' d  hh:mm:ss\')
df.to_excel(writer,sheet_name=\         


        
2条回答
  •  死守一世寂寞
    2021-01-13 07:39

    Some addition to the above.

    Excel zero date is 1-1-1900, while pandas.TimeStamp(0) gives me 1-1-1970.

    So, I changed code to

    df['td_datetime'] = df['td'] + pd.Timestamp('1900-01-01')
    

    and now it works correctly (and you can correctly add cells to add timedeltas)

    Also you might like to display hours only (not 1 day 1 hour, but 25 hours) and for this you can use the following format:

    writer = pd.ExcelWriter('tmp.xlsx', datetime_format='[h]:mm:ss')
    

提交回复
热议问题