'How to remove the timezone from yfinance data?

I grab data with yfinance package. I convert it into a panda dataframe. However, I am unable to save the dataframe to excel file.

ValueError: Excel does not support datetimes with timezones. Please ensure that datetimes are timezone unaware before writing to Excel.

This is how the dataframe looks like. It should be 8 columns. Spyder says it has 7 columns. enter image description here

Below is my codes:

import yfinance as yf
import pandas as pd

stock = yf.Ticker("BABA")
# get stock info
stock.info

# get historical market data
hist = stock.history(start="2021-03-25",end="2021-05-20",interval="15m")
hist = pd.DataFrame(hist)

# pd.to_datetime(hist['Datetime'])
# hist['Datetime'].dt.tz_localize(None)

hist.to_excel(excel_writer= "D:/data/python projects/stock_BABA2.xlsx")


Solution 1:[1]

You can remove the time zone information of DatetimeIndex using DatetimeIndex.tz_localize() , as follows:

hist.index = hist.index.tz_localize(None)

Solution 2:[2]

You can convert time zones using tz_convert(), in your situation it should work with:

hist.index = hist.index.tz_convert(None)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SeaBean
Solution 2 Ryan