Search code examples
pythonpandas-datareader

pandas_datareader.DataReader returns data for just one date


On using the pandas_datareader library to access the S&P 500 historical data, I manage to get the date of JUST the current date and not until the start date as mentioned below.

import pandas as pd
import pandas_datareader as web
import datetime

def test_run():
    end_date = datetime.datetime.today()
    start_date = datetime.date(end_date.year,2,28)


    #DataReader method name is case sensitive
    df=web.DataReader('^SPX','yahoo',start=start_date,end=end_date)
    path_out = 'Data/'
    df.to_csv(path_out+'SPY.csv')    

if __name__ == "__main__":
    test_run()

Date,High,Low,Open,Close,Volume,Adj Close 2020-03-13,2711.330078125,2492.3701171875,2569.989990234375,2711.02001953125,708668739,2711.02001953125

The output is in a csv file as: enter image description here

I cant seem to figure out where the error lies.


Solution

  • The symbol/ticker is '^GSPC', not '^SPX'.

    import pandas as pd
    import datetime
    import pandas_datareader.data as web
    start=datetime.datetime(2019,3,13)
    end=datetime.datetime(2020,3,13)
    df=web.DataReader('^GSPC','yahoo',start,end)
    print(df)
    

    Result:

                       High          Low  ...      Volume    Adj Close
    Date                                  ...                         
    2019-03-13  2821.239990  2799.780029  ...  3766150000  2810.919922
    2019-03-14  2815.000000  2803.459961  ...  3469730000  2808.479980
    2019-03-15  2830.729980  2810.790039  ...  5962730000  2822.479980
    2019-03-18  2835.409912  2821.989990  ...  3552190000  2832.939941
    2019-03-19  2852.419922  2823.270020  ...  3620220000  2832.570068
                    ...          ...  ...         ...          ...
    2020-03-09  2863.889893  2734.429932  ...  8423050000  2746.560059
    2020-03-10  2882.590088  2734.000000  ...  7635960000  2882.229980
    2020-03-11  2825.600098  2707.219971  ...  7374110000  2741.379883
    2020-03-12  2660.949951  2478.860107  ...  8829380000  2480.639893
    2020-03-13  2711.330078  2492.370117  ...  8258670000  2711.020020