why am I getting a "TypeError: String indices Must be integer" Message when trying to access Yahoo Finance? [duplicate]
Asked Answered
V

1

9

I'm following along with a Udemy course for Python & Finance, unfortunately hit a wall whilst trying to call stock data from yahoo using pandas_dataReader.

Here's my code copied directly from jupyter notebook

import numpy as np
import pandas as pd

from pandas_datareader import data as wb
PG = wb.DataReader('PG', data_source='yahoo', start='1995-1-1')

this returns the following - rather lengthy - error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_16532\693660725.py in <module>
----> 1 PG = wb.DataReader('PG', data_source='yahoo', start='1995-1-1')

~\anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    205                 else:
    206                     kwargs[new_arg_name] = new_arg_value
--> 207             return func(*args, **kwargs)
    208 
    209         return cast(F, wrapper)

~\anaconda3\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    368 
    369     if data_source == "yahoo":
--> 370         return YahooDailyReader(
    371             symbols=name,
    372             start=start,

~\anaconda3\lib\site-packages\pandas_datareader\base.py in read(self)
    251         # If a single symbol, (e.g., 'GOOG')
    252         if isinstance(self.symbols, (string_types, int)):
--> 253             df = self._read_one_data(self.url, params=self._get_params(self.symbols))
    254         # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
    255         elif isinstance(self.symbols, DataFrame):

~\anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py in _read_one_data(self, url, params)
    151         try:
    152             j = json.loads(re.search(ptrn, resp.text, re.DOTALL).group(1))
--> 153             data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
    154         except KeyError:
    155             msg = "No data fetched for symbol {} using {}"

TypeError: string indices must be integers
Vitiligo answered 20/12, 2022 at 11:33 Comment(2)
This is a known bug in pandas-datareader. Take a look at this issue: GitHub IssueJackshaft
Does this answer your question? "TypeError: string indices must be integers" when getting data of a stock from Yahoo Finance using Pandas DatareaderHealion
T
15

as @Clasherkasten points out in their comment, you can:

import numpy as np
import pandas as pd

import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)
from pandas_datareader import data as pdr

PG = pdr.get_data_yahoo('PG', start='1995-1-1')

Or else, if you still want to use your code you can get the same information by replacing your data_source = 'yahoo' with data_source = 'stooq'

The information on stooq data source can be found here.

Teratogenic answered 27/12, 2022 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.