0
    import math
    import pandas_datareader.data as web
    import matplotlib.pyplot as plot
    from datetime import datetime
    from matplotlib import style


    style.use('ggplot')

    import csv

    with open('sp500const.csv', newline='') as File:  
    reader = csv.reader(File)
    dataa=[]
    for i in reader:
      dataa.extend(i)

    Dates=[]
    Prices=[]
    Tick=[]
    for stock in dataa[2:100]:
       for year in range(2010,2019):
          if year==2012:  
            start = datetime(year,5,1)
            end=datetime(year,5,1)
            f = web.DataReader(stock, 'morningstar', start, end)
            temp=f['Close']
            Prices.append(float(temp))
            Dates.append(end.strftime('%m/%d/%Y'))
           Tick.append(stock)
         elif year==2013:
            start=datetime(year,5,3)
            end=datetime(year,5,3)
            f = web.DataReader(stock, 'morningstar', start, end)
            temp=f['Close']
            Prices.append(float(temp))
            Dates.append(end.strftime('%m/%d/%Y'))
            Tick.append(stock)
         elif year==2014:
            start=datetime(year,5,5)
            end=datetime(year,5,5)
            f = web.DataReader(stock, 'morningstar', start, end)
            temp=f['Close']
            Prices.append(float(temp))
            Dates.append(end.strftime('%m/%d/%Y'))
            Tick.append(stock)
         else:
            start=datetime(year,5,4)
            end=datetime(year,5,4)
            f = web.DataReader(stock, 'morningstar', start, end)
            temp=f['Close']
            Prices.append(float(temp))
            Dates.append(end.strftime('%m/%d/%Y'))
            Tick.append(stock)


    #datetime(Year,Month,Days)

    x=[Tick,Dates,Prices]
    z=[list(i) for i in zip(*x)]
    print(z)

I want to download data for the S&500 Constituents, whose tickers are in the sp500const.csv file[Tickers as reported from wikipedia.com] for the dates 2011-2018. When i change the number of tickers to be downloaded (for example in the code now, I want to download stock prices for tickers dataa[2:100]). When i choose for instance data[2:4] the code works perfectly. I get the following error:

Traceback (most recent call last):

File "<ipython-input-5-5592627b16bf>", line 44, in <module>
    f = web.DataReader(stock, 'morningstar', start, end)

  File "C:\Users\coxs\Miniconda3\lib\site-packages\pandas_datareader\data.py", line 391, in DataReader
    session=session, interval="d").read()

  File "C:\Users\coxs\Miniconda3\lib\site-packages\pandas_datareader\mstar\daily.py", line 219, in read
    df = self._dl_mult_symbols(symbols=symbols)

  File "C:\Users\coxs\Miniconda3\lib\site-
packages\pandas_datareader\mstar\daily.py", line 126, in _dl_mult_symbols
    jsondata=jsondata)

  File "C:\Users\coxs\Miniconda3\lib\site-packages\pandas_datareader\mstar\daily.py", line 164, in _restruct_json

 volumes = jsondata["VolumeList"]["Datapoints"]

TypeError: 'NoneType' object is not subscriptable
AlexandrosB
  • 133
  • 7

1 Answers1

-1

Use a "try" "except" statement. This should avoid returning the error and stopping the loop. The problem should be from the API output though so you will have missing data.

Saad
  • 11
  • 2