Search code examples
pythonpandasdatetimepandas-datareader

Getting this error trying to retrieve data from Pandas DataReader


Here is my code

def competitor_stock_data_report():
    import datetime
    import pandas_datareader.data as web

    date_time = datetime.datetime.now()
    date = date_time.date()

    stocklist = ['LAZ','AMG','BEN','LM','EVR','GHL','HLI','MC','PJT','MS','GS','JPM','AB']
    start = datetime.datetime(date.year, date.month, date.day)
    end = datetime.datetime(date.year-1, date.month, date.day)

    for x in stocklist:
        df = web.DataReader(x, 'google', start, end)
        print(df['Close'].tail(n=1))
        print(df['Close'].head(n=1))

When I import the date statically, this should work.

This is the error I am getting:

File "pandas\_libs\parsers.pyx", line 565, in pandas._libs.parsers.TextReader.__cinit__ (pandas\_libs\parsers.c:6260)
pandas.errors.EmptyDataError: No columns to parse from file

How can I fix this?


Solution

  • Your end date is before the start date:

    start = datetime.datetime(date.year, date.month, date.day)
    end = datetime.datetime(date.year-1, date.month, date.day)
    

    it should be:

    start = datetime.datetime(date.year-1, date.month, date.day)
    end = datetime.datetime(date.year, date.month, date.day)