Search code examples
pythonpandaspandas-datareader

How can I get the data for the nasdaq index using the pandas data_reader?


Using the Pandas data_reader, I can get the historical stock information in the form of a DataFrame such as:

import pandas_datareader.data as web
import datetime as dt

start = dt.datetime(2015, 1, 1)
end = dt.datetime.now()
df = web.DataReader("TSLA", 'morningstar', start, end)

Can i get the composite data for the NASDAQ index in this way, rather than just one stock?


Solution

  • Your question asked for it using the pandas_datareader module. It used to be very straightforward to get this information using pandas_datareader before Yahoo! altered their API in late 2017 and the csv endpoint was retired. More on some these developments can be in the pandas_datareader docs (link here).

    There is another fairly straightforward option for getting the NASDAQ Composite index into a dataframe. That is, to use the Quandl module for this purpose (Link here).

    Here's how you can use Quandl

    import datetime, quandl
    
    ndq = quandl.get("NASDAQOMX/COMP-NASDAQ", 
                  trim_start='2018-03-01', 
                  trim_end='2018-04-03')
    
    print(ndq.head(4))
    

    With expected Output:

    Trade Date    Index Value     High        Low         Total Market Value
    
    2018-03-01    7180.56         7307.84    7117.66      1.096433e+13
    2018-03-02    7257.87         7267.19    7084.83      1.108254e+13
    2018-03-05    7330.70         7350.07    7205.31      1.119375e+13
    2018-03-06    7372.01         7378.03    7319.68      1.125703e+13