Home > Software engineering >  "TypeError: string indices must be integers" when getting data of a stock from Yahoo Finan
"TypeError: string indices must be integers" when getting data of a stock from Yahoo Finan

Time:12-17

import pandas_datareader

end = "2022-12-15"
start = "2022-12-15"
stock_list = ["TATAELXSI.NS"]

data = pandas_datareader.get_data_yahoo(symbols=stock_list, start=start, end=end)

print(data)

When I run this code, I get error "TypeError: string indices must be integers".

Edit : I have updated the code and passed list as symbol parameter but it still shows the same error

Error :

Traceback (most recent call last):
  File "C:\Users\Deepak Shetter\PycharmProjects\100DAYSOFPYTHON\mp3downloader.py", line 7, in <module>
    data = pandas_datareader.get_data_yahoo(symbols=[TATAELXSI], start=start, end=end)
  File "C:\Users\Deepak Shetter\PycharmProjects\100DAYSOFPYTHON\venv\lib\site-packages\pandas_datareader\data.py", line 80, in get_data_yahoo
    return YahooDailyReader(*args, **kwargs).read()
  File "C:\Users\Deepak Shetter\PycharmProjects\100DAYSOFPYTHON\venv\lib\site-packages\pandas_datareader\base.py", line 258, in read
    df = self._dl_mult_symbols(self.symbols)
  File "C:\Users\Deepak Shetter\PycharmProjects\100DAYSOFPYTHON\venv\lib\site-packages\pandas_datareader\base.py", line 268, in _dl_mult_symbols
    stocks[sym] = self._read_one_data(self.url, self._get_params(sym))
  File "C:\Users\Deepak Shetter\PycharmProjects\100DAYSOFPYTHON\venv\lib\site-packages\pandas_datareader\yahoo\daily.py", line 153, in _read_one_data
    data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
TypeError: string indices must be integers

CodePudding user response:

Because symbols parameter in get_data_yahoo() expects a list and a str was given.

import pandas_datareader

end = "2022-12-15"
start = "2022-12-15"
TATAELXSI = "TATAELXSI.NS"

data = pandas_datareader.get_data_yahoo(symbols=[TATAELXSI], start=start, end=end)

print(data)

Alternative:

TATAELXSI = ["TATAELXSI.NS"]

data = pandas_datareader.get_data_yahoo(symbols=TATAELXSI, start=start, end=end)

CodePudding user response:

This is not an answer, but I think the problem is related with pdr data reader fetching from yahoo itself

>>> import pandas_datareader as dtr
>>> from datetime import datetime
>>> initial_portfolio=['AAPL', 'MA', 'F', 'MSFT', '^GSPC']
>>> startdate = datetime(2022,12,1)
>>> enddate=datetime(2022,12,10)
>>> stock_data=dtr.yahoo.daily.YahooDailyReader(initial_portfolio,start=startdate,end=enddate).read()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lib/python3.9/site-packages/pandas_datareader/base.py", line 258, in read
    df = self._dl_mult_symbols(self.symbols)
  File "lib/python3.9/site-packages/pandas_datareader/base.py", line 268, in _dl_mult_symbols
    stocks[sym] = self._read_one_data(self.url, self._get_params(sym))
  File "lib/python3.9/site-packages/pandas_datareader/yahoo/daily.py", line 153, in _read_one_data
    data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
TypeError: string indices must be integers

and the short term fix could be to use yfinance override and see if that helps in the meanwhile until yahoo finance restores their data functionality?

Python 3.9.1 (default, Dec 28 2020, 11:22:14)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pandas_datareader import data as pdr
>>> import yfinance as yf
>>> yf.pdr_override()
>>> y_symbols = ['SCHAND.NS', 'TATAPOWER.NS', 'ITC.NS']
>>> from datetime import datetime
>>> startdate = datetime(2022,12,1)
>>> enddate = datetime(2022,12,15)
>>> data = pdr.get_data_yahoo(y_symbols, start=startdate, end=enddate)
[*********************100%***********************]  3 of 3 completed
>>> data
             Adj Close                                Close                           ...        Open                             Volume
                ITC.NS   SCHAND.NS TATAPOWER.NS      ITC.NS   SCHAND.NS TATAPOWER.NS  ...      ITC.NS   SCHAND.NS TATAPOWER.NS    ITC.NS SCHAND.NS TATAPOWER.NS
Date                                                                                  ...
2022-12-01  339.549988  195.949997   224.850006  339.549988  195.949997   224.850006  ...  341.700012  191.600006   225.250000  16630417    544485      7833074
2022-12-02  337.149994  196.600006   225.250000  337.149994  196.600006   225.250000  ...  339.350006  196.000000   225.449997   8388835    122126      7223274
2022-12-05  336.750000  191.050003   224.199997  336.750000  191.050003   224.199997  ...  337.649994  200.850006   225.250000   9716390    107294     10750610
2022-12-06  337.299988  196.399994   228.800003  337.299988  196.399994   228.800003  ...  334.100006  191.000000   224.199997   6327430    102911     20071039
2022-12-07  340.100006  187.350006   225.850006  340.100006  187.350006   225.850006  ...  338.500000  198.000000   228.800003   9813208    122772      7548312
2022-12-08  338.399994  181.850006   225.050003  338.399994  181.850006   225.050003  ...  340.200012  186.000000   226.000000   6200447    114147      7507975
2022-12-09  341.399994  176.899994   219.399994  341.399994  176.899994   219.399994  ...  339.750000  183.899994   225.899994   8132228    179660     13087278
2022-12-12  343.200012  177.350006   217.699997  343.200012  177.350006   217.699997  ...  341.000000  177.750000   219.750000  11214662    133507      8858525
2022-12-13  345.600006  178.449997   218.850006  345.600006  178.449997   218.850006  ...  344.500000  179.350006   218.800003  10693426     74873      7265105
2022-12-14  345.399994  179.149994   222.699997  345.399994  179.149994   222.699997  ...  346.000000  180.449997   219.800003   7379878     32085      9179593

[10 rows x 18 columns]
>>>

CodePudding user response:

You are passing a string value as the symbols parameter. That should be a list of symbols, not string

end = "2022-12-15"
start = "2022-12-15"

data = pandas_datareader.get_data_yahoo(symbols=["TATAELXSI.NS"], start=start, end=end)

print(data)
  • Related