I am just starting to code python on Jupyter and I can't seem to solve this problem. I was trying to import data from Yahoo Finance but it doesn't let me. I don't know if I have to install a new version of Pandas, because I already did. Maybe it has something to do with the function being old, I don't know.
Here's the code and the message of error:
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')
Error from the line of code above:
TypeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 PG = wb.DataReader('Pg', data_source='yahoo', start='1995-1-1')
File ~/anaconda3/lib/python3.11/site-packages/pandas/util/_decorators.py:213, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs)
211 raise TypeError(msg)
212 kwargs[new_arg_name] = new_arg_value
--> 213 return func(*args, **kwargs)
File ~/anaconda3/lib/python3.11/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":
382 return IEXDailyReader(
383 symbols=name,
384 start=start,
(...)
390 session=session,
391 ).read()
File ~/anaconda3/lib/python3.11/site-packages/pandas_datareader/base.py:253, in _DailyBaseReader.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):
File ~/anaconda3/lib/python3.11/site-packages/pandas_datareader/yahoo/daily.py:153, in YahooDailyReader._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, not 'str'
Generally speaking i use the
yfinancemodule for yahoo finance, which can be found here: https://pypi.org/project/yfinanceI find the API more convenient.
here is some boiler plate that i start with:
But there is a whole bunch of other stuff that one can do.