Home > Software design >  pdr. Datareader - TypeError: string indices must be integers
pdr. Datareader - TypeError: string indices must be integers

Time:12-19

This code worked just fine until today. Now i get this Error message: TypeError: string indices must be integers

import pandas_datareader as pdr
Equity_Indices = ['^GSPC', 'ES=F', 'NQ=F', 'YM=F', '^RUT', '^DJT', 
                  '^GDAXI', '^N225', '^SSMI', '^STOXX50E', '^FCHI', 
                  '^GSPTSE', '^HSI', '000001.SS', '^KS11', '^NSEI', '^AXJO']
Equity_Indices_df = pdr.DataReader(Equity_Indices, 'yahoo', start='1990-01-01', end='today')

What happened? I am kinda lost here.

I am completely lost, I did not change a thing. this code worked just fine yesterday.

CodePudding user response:

My code has also stopped working.

It appears that yahoo has changed their interface. In the immortal words of Dr. McCoy: "I know engineers. They love to change things."

I've made the following changes to my code:

import yfinance as yf

yf.pdr_override()

df = yf.download(ticker, dateStart, dateEnd)

This has created another minor nuisance, the columns come in a different order now. Whereas before they were:

Date, High, Low, Open, Close, Volume, Adj Close

They are now:

Date, Open, High, Low, Close, Adj Close, Volume

This shouldn't be a problem unless you're hardcoding all your column code, so beware.

I haven't done any benchmarks, but the download feels faster.

CodePudding user response:

So, if you absolutely must use pandas_datareader you will need to downgrade it to:

pip install pandas_datareader==0.9.0

Your code will run

import pandas_datareader as pdr 
import timedelta
import pandas as pd
from datetime import date

Equity_Indices = ['^GSPC', 'ES=F', 'NQ=F', 'YM=F', '^RUT', '^DJT', '^GDAXI', '^N225', '^SSMI', '^STOXX50E', '^FCHI', '^GSPTSE', '^HSI', '000001.SS', '^KS11', '^NSEI', '^AXJO']

print(pdr.__version__)
from datetime import datetime
startdate = '1990-01-01'
today = date.today()
enddate = today

dd = []
for symbol in Equity_Indices:
    try:
        Equity_Indices_df = pdr.DataReader(symbol,'yahoo',startdate,enddate)
        print(Equity_Indices_df.head(5))
        dd.append(Equity_Indices_df)
    except:
        print('did not find: ' symbol)

but will return:

did not find: ^GSPC
did not find: ES=F
did not find: NQ=F
did not find: YM=F
did not find: ^RUT
did not find: ^DJT
did not find: ^GDAXI
did not find: ^N225
did not find: ^SSMI
did not find: ^STOXX50E
did not find: ^FCHI
did not find: ^GSPTSE
did not find: ^HSI
did not find: 000001.SS
did not find: ^KS11
did not find: ^NSEI
did not find: ^AXJO

This means that Yahoo has changed something on the page from which to retreive symbol data.

Today, the only fix is to do the following:

You need to use y_finance and use pdr_overide()

from pandas_datareader import data as pdr
from datetime import datetime,date

import yfinance as yf
yf.pdr_override()

y_symbols = ['^GSPC', 'ES=F', 'NQ=F', 'YM=F', '^RUT', '^DJT', '^GDAXI', '^N225', '^SSMI', '^STOXX50E', '^FCHI', '^GSPTSE', '^HSI', '000001.SS', '^KS11', '^NSEI', '^AXJO']

from datetime import datetime
startdate = datetime(1990,1,1)
today = date.today()
enddate = today


data = pdr.get_data_yahoo(y_symbols, start=startdate, end=enddate)

which returns:

print(data.head(2)

   Adj Close                                                           \
           000001.SS ES=F NQ=F YM=F ^AXJO ^DJT ^FCHI       ^GDAXI       ^GSPC   
Date                                                                            
1990-01-02       NaN  NaN  NaN  NaN   NaN  NaN   NaN  1788.890015  359.690002   
1990-01-03       NaN  NaN  NaN  NaN   NaN  NaN   NaN  1867.290039  358.760010   

                         ... Volume                                          \
                ^GSPTSE  ... ^GDAXI        ^GSPC   ^GSPTSE ^HSI ^KS11 ^N225   
Date                     ...                                                  
1990-01-02  3994.230957  ...    0.0  162070000.0  164600.0  0.0   NaN   NaN   
1990-01-03  3999.317871  ...    0.0  192330000.0  147600.0  0.0   NaN   NaN   

                                               
           ^NSEI         ^RUT ^SSMI ^STOXX50E  
Date                                           
1990-01-02   NaN  162070000.0   NaN       NaN  
1990-01-03   NaN  192330000.0   NaN       NaN  

[2 rows x 102 columns]

  • Related