Search code examples
pythonpandasyfinance

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

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

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