Home » excel » python – Pandas: to_excel(): How can I express Time as a format to TimeSeries index?

python – Pandas: to_excel(): How can I express Time as a format to TimeSeries index?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a DataFrame with an index as:

Index([00:00:00, 00:05:00, 00:10:00, 00:15:00, 00:20:00, 00:25:00, 00:30:00,
   00:35:00, 00:40:00, 00:45:00,
   ...
   23:10:00, 23:15:00, 23:20:00, 23:25:00, 23:30:00, 23:35:00, 23:40:00,
   23:45:00, 23:50:00, 23:55:00],
  dtype='object', length=288)

I write this df into an excel as follows:

writer = pd.ExcelWriter("output.xlsx")
df.set_index(index, inplace=True)
df.to_excel(writer, sheet_name="Days")
writer.save()

But when I open and look at the Excel contents I see this:

enter image description here

The index appears as floating point numbers.

If I format the Excel cells to time then I see it like this:

enter image description here

I don’t like to manually format the sheets every time I open them, so, is there a way to express formatting when I’m creating the Excel file?

How to&Answers:

If you use XlsxWriter as your engine for Pandas, you can set the formatting of specific columns in this way:

workbook = writer.book
worksheet = writer.sheets['Days']
timeFormat = workbook.add_format({'num_format': 'h:mm:ss'})
worksheet.set_column('A:A', None, timeFormat)

Source

Answer:

You can do it using datetime_format argument of the pandas.ExcelWriter() function, but you would need to cast your index to datetime type first:

In [104]: df
Out[104]:
          val
00:00:00    4
00:05:00    9
00:10:00    7
00:15:00    7
00:20:00    7
00:25:00    0
00:30:00    7
00:35:00    4
00:40:00    3
00:45:00    1

In [105]: df.index = pd.to_datetime(df.index, format='%H:%M:%S')

In [106]: df
Out[106]:
                     val
1900-01-01 00:00:00    4
1900-01-01 00:05:00    9
1900-01-01 00:10:00    7
1900-01-01 00:15:00    7
1900-01-01 00:20:00    7
1900-01-01 00:25:00    0
1900-01-01 00:30:00    7
1900-01-01 00:35:00    4
1900-01-01 00:40:00    3
1900-01-01 00:45:00    1

In [107]: writer = pd.ExcelWriter("d:/temp/output.xlsx", datetime_format='hh:mm:ss')

In [108]: df.to_excel(writer)

In [109]: writer.save()

If you don’t want to change your index – you can do it ‘on-the-fly’ when writing to the Excel file, thus preserving the original index:

df.set_index(pd.to_datetime(df.index, format='%H:%M:%S')).to_excel(writer)

Resulting Excel file:

enter image description here