问题
I have microsecond resolution in my df which is very important but no matter what I try, I can't get excel to show microsecond resolution with either .xls or .xlsx. Any ideas on how to get them to display without converting to a string explicitly?
回答1:
With the latest version of Pandas on GitHub (and in the soon to be released 0.13.1) you can specify the Excel date format in the ExcelWriter()
like this:
import pandas as pd
from datetime import datetime
df = pd.DataFrame([datetime(2014, 2, 1, 12, 30, 5, 60000)])
writer = pd.ExcelWriter("time.xlsx", date_format='hh:mm:ss.000')
df.to_excel(writer, "Sheet1")
writer.close()
Which will display the microsecond times (or at least milliseconds since that is Excel's display limit):
See also Working with Python Pandas and XlsxWriter.
来源:https://stackoverflow.com/questions/21650362/pandas-output-timestamps-to-excel-with-microseconds