close

[Solved] ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel Error Occurs?

Today I get the following error ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel in Python.

How To Solve ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel Error ?

  1. How To Solve ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel Error ?

    To Solve ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel Error I found this way easier and more dynamic. This solution you select the columns by the type and applied the desire conversion.

  2. ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel

    To Solve ExcelWriter ValueError: Excel does not support datetime with timezone when saving df to Excel Error I found this way easier and more dynamic. This solution you select the columns by the type and applied the desire conversion.

Solution 1

This should do the job, remove timezone from columns before exporting to excel (using tz_localize(None)).

# Check which columns have timezones datetime64[ns, UTC] 
df.dtypes

# Remove timezone from columns
df['date'] = df['date'].dt.tz_localize(None)

# Export to excel
df.to_excel('filename.xlsx')

Solution 2

I found this way easier and more dynamic. This solution you select the columns by the type and applied the desire conversion.

date_columns = df.select_dtypes(include=['datetime64[ns, UTC]']).columns
for date_column in date_columns:
    df[date_column] = df[date_column].dt.date
    
df.to_excel('anbima_feed.xlsx',engine='xlsxwriter')

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment