[Python] How to remove the timezone from yfinance data?

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    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. [​IMG]

    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")

    Continue reading...

Compartilhe esta Página