Caching queries#

Making the same request repeatedly can use a lot of bandwidth, slow down your code and may result in your IP being banned.

pandas-datareader allows you to cache queries using requests_cache by passing a requests_cache.Session to DataReader using the session parameter.

Below is an example with FRED. The session parameter is implemented for the maintained public data readers.

In [1]: import pandas_datareader.data as web

In [2]: import datetime

In [3]: import requests_cache

In [4]: expire_after = datetime.timedelta(days=3)

In [5]: session = requests_cache.CachedSession(cache_name='cache', backend='sqlite', expire_after=expire_after)

In [6]: start = datetime.datetime(2010, 1, 1)

In [7]: end = datetime.datetime(2013, 1, 27)

In [8]: f = web.DataReader("VIXCLS", 'fred', start, end, session=session)

In [9]: f.head()
Out[9]: 
            VIXCLS
DATE              
2010-01-01     NaN
2010-01-04   20.04
2010-01-05   19.35
2010-01-06   19.16
2010-01-07   19.06

A SQLite file named cache.sqlite will be created in the working directory, storing the request until the expiry date.

For additional information on using requests-cache, see the documentation.