Error "string indices must be integer" when selecting yahoo finance stock data with "start" and "end" dates

Question:

I want to create a simple script that pulls stock data from yahoo finance with pandas_datareader package:

from pandas_datareader import data

stocks = data.DataReader('MSFT', 'yahoo', start='2020-01-14', end='2023-01-14')

but the code gets me "string indices must be integers" TypeError message

TypeError                                 Traceback (most recent call last)
Cell In[1], line 3
      1 from pandas_datareader import data
----> 3 stocks = data.DataReader('MSFT', 'yahoo', start='2020-01-14', end='2023-01-14')

File c:Usersder_Lanaconda3envspy39libsite-packagespandasutil_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs)
    209     else:
    210         kwargs[new_arg_name] = new_arg_value
--> 211 return func(*args, **kwargs)

File c:Usersder_Lanaconda3envspy39libsite-packagespandas_datareaderdata.py:379, in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    367     raise NotImplementedError(msg)
    369 if data_source == "yahoo":
    370     return YahooDailyReader(
    371         symbols=name,
    372         start=start,
    373         end=end,
    374         adjust_price=False,
    375         chunksize=25,
    376         retry_count=retry_count,
    377         pause=pause,
    378         session=session,
--> 379     ).read()
    381 elif data_source == "iex":
...
--> 153     data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
    154 except KeyError:
    155     msg = "No data fetched for symbol {} using {}"

TypeError: string indices must be integers

The script is almost the same as many seen on the web but I can’t understand what’s wrong with it.

Asked By: der_Leo

||

Answers:

You are using strings for the start and end dates. Use:

import datetime  
  
start_date = datetime.datetime(2020, 1, 14)
end_date = datetime.datetime(2023, 1, 14)

Then use these in the DataReader call

Answered By: user19077881

It looks like currently there is a change that is not updated on pandas-datareader yet. The bug report can be found here. As a workaround you can use yfinance package as shown below. For this to work you should install yfinance package with pip install yfinance.

Then, below modified code should set everything going.

from pandas_datareader import data
import yfinance as yfin

yfin.pdr_override()

stocks = data.get_data_yahoo('MSFT', start='2020-01-14', end='2023-01-14')

Answered By: zukunft