Home > Enterprise >  Getting typeerror when trying to use pandas datareader
Getting typeerror when trying to use pandas datareader

Time:12-18

I'm trying to run the following code after the necessary library imports:

import numpy as np
import pandas as pd
import yfinance as yf
import pandas_datareader as web
import datetime as dt

tickers = ['BAC','JPM','C','NSRGY']
start_date = dt.datetime(2019,2,1)
end_date = dt.datetime(2019,12,30)
data = web.DataReader(tickers, 'yahoo', start_date, end_date)

And I end up with this TypeError which I don't quite understand (not posting the whole error):

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/pv/98lyfc8n6tzbdrydqyjpt2qm0000gn/T/ipykernel_3109/4067046804.py in <module>
      2 start_date = dt.datetime(2019,2,1)
      3 end_date = dt.datetime(2019,12,30)
----> 4 data = web.DataReader(tickers, 'yahoo', start_date, end_date)


TypeError: string indices must be integers

The code seemed to be working for me the other day when I used it, perhaps I've made a silly mistake somewhere.

CodePudding user response:

There was someone with your same problem here.

Looks like it's a problem of pandas datareader fetching Yahoo Finance info, in your case you could use this code to obtain the data:

from pandas_datareader import data as pdr
import yfinance as yf
import datetime as dt

yf.pdr_override()
tickers = ['BAC','JPM','C','NSRGY']
start_date = dt.datetime(2019,2,1)
end_date = dt.datetime(2019,12,30)
data = pdr.get_data_yahoo(tickers,start=start_date, end=end_date)
  • Related