'TypeError: string indices must be integers' on Using web.DataReader with yahoo Finance API

92 views Asked by At

import pandas_datareader as webreader
import pandas as pd
import matplotlib.pyplot as plt

# Set the API 
data_source = "yahoo"

# Set the API parameters
date_today = "2020-01-01" # period start date
date_start = "2010-01-01" # period end date
symbol = "GOOG" # asset symbol - For more symbols check yahoo.finance.com




# Send the request to the yahoo finance api endpoint
df = webreader.DataReader(symbol, start=date_start, end=date_today, data_source=data_source)
df.head(5)

I get the following error !!

TypeError                                 Traceback (most recent call last)
<ipython-input-54-261be5f54b08> in <cell line: 2>()
      1 # Send the request to the yahoo finance api endpoint
----> 2 df = webreader.DataReader(symbol, start=date_start, end=date_today, data_source=data_source)
      3 df.head(5)

3 frames
/usr/local/lib/python3.10/dist-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    209                 else:
    210                     kwargs[new_arg_name] = new_arg_value
--> 211             return func(*args, **kwargs)
    212 
    213         return cast(F, wrapper)

/usr/local/lib/python3.10/dist-packages/pandas_datareader/data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    377             pause=pause,
    378             session=session,
--> 379         ).read()
    380 
    381     elif data_source == "iex":

/usr/local/lib/python3.10/dist-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):

/usr/local/lib/python3.10/dist-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

Using web.DataReader() but getting the error that the string indices must be integers. tried ['GOOG', 'AAPL', 'MSFT'] in symbol as well. I need to Use the yahoo finance api to get 'BTC-USD' dataset.

1

There are 1 answers

0
Priyanshu Jha On

Yahoo have recently made changes to their API so currently the code provided is unable to provide the dataset. You have to use yfinance through pandas_datareader -

from pandas_datareader import data as pdr

import yfinance as yf 

yf.pdr_override()

data = pdr.get_data_yahoo("GOOG", start="2020-01-01", end="2020-01-01")

print(data.head())

Full article - link

Thanks