0

When exporting a DataFrame to excel, the index is formated in quarters (2001-Q1,2001-Q2...). However, when I export it to excel (through ExcelWriter), it looses the format. How can I maintain the quarter`s format when exporting it to excel.

For reference, this was used for formatting:

df['Quarter'] = df['ISSUE_DATE'].dt.to_period('Q')
df = df.set_index('Quarter')

with pd.ExcelWriter("file_name.xlsx") as writer:  
    df.to_excel(writer, sheet_name='Quarterly Dataset', index=True)

What comes out in Excel: enter image description here

Sumit
  • 99
  • 6
  • 1
    Try `df['Quarter'] = df['ISSUE_DATE'].dt.to_period('Q').astype(str)`. "2001-Q1", "2001-Q2", etc. are just strings in Excel. – BigBen Mar 01 '22 at 20:20
  • @BigBen despite using this method, excel still manages to reformat to the same issue mentioned initially. – Sumit Mar 01 '22 at 21:20
  • I can't reproduce this. Can you provide sample data for `df['Quarter']`, or maybe `df['ISSUE_DATE']`? – BigBen Mar 01 '22 at 21:22

0 Answers0