Reputation: 77
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.
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")
Upvotes: 3
Views: 9085
Reputation: 9
You can convert time zones using tz_convert()
, in your situation it should work with:
hist.index = hist.index.tz_convert(None)
Upvotes: 0
Reputation: 23227
You can remove the time zone information of DatetimeIndex
using DatetimeIndex.tz_localize()
, as follows:
hist.index = hist.index.tz_localize(None)
Upvotes: 6