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

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.

Leave a Comment

tech