Jordan Calyton
Jordan Calyton

Reputation: 11

Getting the "TypeError: string indices must be integers" message with Pandas DataReader

I am using DataReader from pandas to import financial data from yahoo and would like to mention that I have used the code in the first image here with no issues a few weeks ago. When I opened up this file and tried to run it today I am getting the "TypeError: string indices must be integers" message.

1

# Load Packages
import numpy as np
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
%matplotlib inline 

# Read Data
test = web.DataReader(['TSLA','FB'], 'yahoo', start='2018/01/01', end='2019/12/31')
test.head()

Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3456/2405099378.py in <module>
      1 # Read Data
----> 2 test = web.DataReader(['TSLA','FB'], 'yahoo', start='2018/01/01', end='2019/12/31')
      3 test.head()

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    205                 else:
    206                     kwargs[new_arg_name] = new_arg_value
--> 207             return func(*args, **kwargs)
    208 
    209         return cast(F, wrapper)

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    376             retry_count=retry_count,
    377             pause=pause,
--> 378             session=session,
    379         ).read()
    380 

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas_datareader\base.py in read(self)
    256             df = self._dl_mult_symbols(self.symbols.index)
    257         else:
--> 258             df = self._dl_mult_symbols(self.symbols)
...
--> 153             data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
    154         except KeyError:
    155             msg = "No data fetched for symbol {} using {}"

TypeError: string indices must be integers

I have even copy pasted the same code directly from pandas documentation for DataReader and it gives the same error. Something must have updated the way this function reads the data, however, I can't find the change in the docs. Has anyone else been able to solve this issue?

2

In [39]: import pandas_datareader.data as web

In [40]: import pandas as pd

In [41]: import datetime as dt

In [42]: df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')

In [43]: df.head()

Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3456/472188857.py in <module>
----> 1 df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')
      2 
      3 df.head()

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    205                 else:
    206                     kwargs[new_arg_name] = new_arg_value
--> 207             return func(*args, **kwargs)
    208 
    209         return cast(F, wrapper)

c:\Users\jclay\anaconda3\envs\blockchain_dev\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    376             retry_count=retry_count,
    377             pause=pause,
--> 378             session=session,
    379         ).read()
    380 

c:\Users\jclay\anaconda3\envs\blockchain_dev\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))
...
--> 153             data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
    154         except KeyError:
    155             msg = "No data fetched for symbol {} using {}"

TypeError: string indices must be integers

Upvotes: 0

Views: 854

Answers (1)

Satyam Singh
Satyam Singh

Reputation: 11

I got the same error with my code too, Yahoo Finance have changed their formating of data cause of which DataReader is not able to get the required stock info. I used yfinance which is same as DataReader data just that this is a free library.You can go here to see all the syntax.


Syntax :

import yfinance as yf
df = yf.download('ticker_name', start, end)

Your code:

df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')

Instead :

import yfinance as yf 
df = yf.download('GE', start='2019-09-10', end='2019-10-09')

Upvotes: 1

Related Questions