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]: 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("F", 'yahoo', start, end, session=session)
---------------------------------------------------------------------------
ImmediateDeprecationError                 Traceback (most recent call last)
<ipython-input-8-7059d99fc9b4> in <module>()
----> 1 f = web.DataReader("F", 'yahoo', start, end, session=session)

~/checkouts/readthedocs.org/user_builds/pandas-datareader/envs/latest/lib/python3.5/site-packages/pandas_datareader-0.6.0-py3.5.egg/pandas_datareader/data.py in DataReader(name, data_source, start, end, retry_count, pause, session, access_key)
    289     """
    290     if data_source == "yahoo":
--> 291         raise ImmediateDeprecationError(DEP_ERROR_MSG.format('Yahoo Daily'))
    292         return YahooDailyReader(symbols=name, start=start, end=end,
    293                                 adjust_price=False, chunksize=25,

ImmediateDeprecationError: 
Yahoo Daily has been immediately deprecated due to large breaks in the API without the
introduction of a stable replacement. Pull Requests to re-enable these data
connectors are welcome.

See https://github.com/pydata/pandas-datareader/issues


In [9]: f.ix['2010-01-04']
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-f76d00c0d565> in <module>()
----> 1 f.ix['2010-01-04']

NameError: name 'f' is not defined

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.