7

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
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
boovey
  • 71
  • 1
  • 3
  • This is a known bug in pandas-datareader. Take a look at this issue: [GitHub Issue](https://github.com/pydata/pandas-datareader/issues/952) – Clasherkasten Dec 20 '22 at 11:38
  • 2
    Does this answer your question? ["TypeError: string indices must be integers" when getting data of a stock from Yahoo Finance using Pandas Datareader](https://stackoverflow.com/questions/74832296/typeerror-string-indices-must-be-integers-when-getting-data-of-a-stock-from-y) – Tomerikoo Jan 04 '23 at 15:01

1 Answers1

13

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.

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
FFFAR
  • 141
  • 5