pythonyahoo-financepandas-datareader

"TypeError: string indices must be integers" when getting data of a stock from Yahoo Finance using Pandas Datareader


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

Solution

  • None of the solutions reported here so far worked for me. As per the discussion here Yahoo made changes to their API that broke compatibility with previous pandas datareader versions.

    In the same Github thread a fix is reported, implemented in a pull request from Github user raphi6. I confirmed the pull request works fine. The version from the pull request can be installed with this 3 lines:

    conda install pycryptodome pycryptodomex
    conda uninstall pandas-datareader
    pip install git+https://github.com/raphi6/pandas-datareader.git@ea66d6b981554f9d0262038aef2106dda7138316
    

    The pycrypto* packages are dependencies I have to install to make it work. Notice I am using the commit hash here instead of the branch name, because it is Yahoo!_Issue#952 and there is an issue with hash characters when using pip this way.

    This can also be done using pip for all the commands instead of conda (see Update 1 below).

    Update 1

    To try this on Google Colab use (as shown here):

    ! pip install pycryptodome pycryptodomex
    ! pip uninstall --yes pandas-datareader
    ! pip install git+https://github.com/raphi6/pandas-datareader.git@ea66d6b981554f9d0262038aef2106dda7138316
    

    Update 2 (27/12/2022)

    Although past week I could not make it work, I have tried again, and I can confirm that the pdr_override() workaround mentioned below by Nikhil Mulley is working now (at least with yfinance 0.2.3 and pandas-datareader 0.10.0).

    Original answer (works but more lines of code)

    In the same Github thread a fix is reported, implemented in a pull request from Github user raphi6. I confirmed the pull request works fine. Detailed installation instructions for the pull request can be found here, copied below for the sake of completeness:

    git clone https://github.com/raphi6/pandas-datareader.git
    cd pandas-datareader
    conda uninstall pandas-datareader
    conda install pycryptodome pycryptodomex
    git checkout 'Yahoo!_Issue#952'
    python setup.py install --record installed_files.txt
    

    The --record argument in the install command is to get a list of installed files, so that it is easy to uninstall in the future (following this SO thread). The pycrypto* files are dependencies I have to install to make it work.