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:\Users\der_L\anaconda3\envs\py39\lib\site-packages\pandas\util\_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:\Users\der_L\anaconda3\envs\py39\lib\site-packages\pandas_datareader\data.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.
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')