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 or Options using the session parameter.

Below is an example with Yahoo! Finance. The session parameter is implemented for all datareaders.

In [1]: import pandas_datareader.data as web

In [2]: from pandas_datareader.yahoo.headers import DEFAULT_HEADERS

In [3]: import datetime

In [4]: import requests_cache

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

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

In [7]: session.headers = DEFAULT_HEADERS

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

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

In [10]: f = web.DataReader("F", 'yahoo', start, end, session=session)

In [11]: f.loc['2010-01-04']
Out[11]: 
High         1.028000e+01
Low          1.005000e+01
Open         1.017000e+01
Close        1.028000e+01
Volume       6.085580e+07
Adj Close    6.968545e+00
Name: 2010-01-04 00:00:00, dtype: float64

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.