Search code examples
pythonyahoo-finance

unpacking yahoo finance historical pice


Using yahoo finance I have downloaded, the historical data for Apple. I am using the information from https://pypi.org/project/yahoofinancials/

from yahoofinancials import YahooFinancials

ticker = 'AAPL'
yahoo_financials = YahooFinancials(ticker)

From this I can use the below to get weekly data:

historical_price = yahoo_financials.get_historical_price_data('2020-01-05', '2021-01-05', 'weekly')

This works well. But I am struggling with unpacking this into pandas. There are few dictionaries of dictionaries and list of dictionaries that have got me stumped.

The current output is

                 AAPL
currency                                                      USD
eventsData                                                     {}
firstTradeDate  {'formatted_date': '1980-12-12', 'date': 34547...
instrumentType                                             EQUITY
prices          [{'date': 1609736400, 'high': 133.610000610351...
timeZone                                    {'gmtOffset': -18000} 

My question is what the best way to unpack the above into a usable dataframe is?


Solution

  • I would suggest using yfinance instead which is also yahoo based:

    !pip install yfinance
    import yfinance
    yfinance.download('AAPL', start='2019-01-01', end='2020-12-31')
    

    This outputs:

                      Open        High  ...   Adj Close     Volume
    Date                                ...                       
    2019-01-02   38.722500   39.712502  ...   38.562561  148158800
    2019-01-03   35.994999   36.430000  ...   34.721451  365248800
    2019-01-04   36.132500   37.137501  ...   36.203678  234428400
    2019-01-07   37.174999   37.207500  ...   36.123104  219111200
    2019-01-08   37.389999   37.955002  ...   36.811718  164101200
    ...                ...         ...  ...         ...        ...
    2020-12-23  132.160004  132.429993  ...  130.960007   88223700
    2020-12-24  131.320007  133.460007  ...  131.970001   54930100
    2020-12-28  133.990005  137.339996  ...  136.690002  124486200
    2020-12-29  138.050003  138.789993  ...  134.869995  121047300
    2020-12-30  135.580002  135.990005  ...  133.720001   96452100
    

    This would be exact same as using:

    yahoo_financials = YahooFinancials(ticker)
    historical_price = yahoo_financials.get_historical_price_data('2020-01-05', '2021-01-05', 'weekly')
    pd.DataFrame(historical_price['AAPL']['prices'])