TypeError: string indices must be integers
I input the following:
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
PG = wb.DataReader('PG',data_source = 'yahoo',start = '1995-1-1')
The following error message appeared:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_6012\1888070805.py in <module>
----> 1 PG = wb.DataReader('PG',data_source = 'yahoo',start = '2000-1-1', end = '2001-1-1')
~\anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
205 f"Can only specify {repr(old_arg_name)} "
206 f"or {repr(new_arg_name)}, not both."
--> 207 )
208 raise TypeError(msg)
209 else:
~\anaconda3\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
368
369 if data_source == "yahoo":
--> 370 return YahooDailyReader(
371 symbols=name,
372 start=start,
~\anaconda3\lib\site-packages\pandas_datareader\base.py in read(self)
251 # If a single symbol, (e.g., 'GOOG')
252 if isinstance(self.symbols, (string_types, int)):
--> 253 df = self._read_one_data(self.url, params=self._get_params(self.symbols))
254 # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
255 elif isinstance(self.symbols, DataFrame):
~\anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py in _read_one_data(self, url, params)
151 try:
152 j = json.loads(re.search(ptrn, resp.text, re.DOTALL).group(1))
--> 153 data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
154 except KeyError:
155 msg = "No data fetched for symbol {} using {}"
TypeError: string indices must be integers
May I know how to solve the error?
Hi Wing!
Thanks for reaching out.
Yahoo have recently made changes to their API so currently the code provided is unable to provide the dataset as shown in the video lectures. Potentially, this will only be a temporary issue (as have happened in the past), meaning that once Yahoo fix the related issues themselves, the code will start working again.
Of course, this depends entirely on their data teams, so we cannot guarantee the future outcome of these changes with certainty. Therefore, to ensure you smooth continuation with taking this course, please consider applying one of the following options (suggestions provided in order of preference according to us):
- use the *.csv files we've provided to the lectures
- use yfinance instead of pandas-datareader
- use pycryptodome or pycryptodomex (risking to use data that is not as reliable as of now)
Hope this helps.
Kind regards,
Martin
from pandas_datareader import data as pdr #import pandas datareader
import yfinance as yf # you will need to install it if haven't done it already
#override the data reader function
yf.pdr_override()
data = pdr.get_data_yahoo("TSLA", start="2022-12-01", end="2023-02-08") # I got results for TSLA but you can change whatever you like
Hi Muhammad!
Thanks for reaching out and sharing this solution with the Community.
Good luck and please feel free to post another question should you encounter any difficulties. Thank you.
Best,
Martin