Search code examples
pythoncachingmatplotlibyahoo-finance

avoiding the local cache when fetching yahoo finance data from matplotlib.finance in python


I am fetching yahoo finance data in Python through this interface:

matplotlib.finance.quotes_historical_yahoo_ohlc(ticker, start_date, end_date)

I find this interface very convenient as prices are automatically adjusted, and the data is made available in a convenient data structure.

However, I recently found that matplotlib.finance uses a local cache (matplotlib.get_cachedir() tells me my cache is in ~/.matplotlib). I would like to avoid using any sort of local cache as I want to ensure that any data I request is not stale. But I am unable to figure out an option to turn local caching off. At the very least, I would like to understand the controls that reset this local cache in ~/.matplotlib.


Solution

  • I have found in source code of matplotlib, that, if get_cachedir returns None, that cache will not be used (https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/finance.py). So you can monkeypatch method get_cachedir like this:

    import matplotlib 
    
    def empty_get_cachedir(*args, **kwargs):
        return None
    
    matplotlib.get_cachedir = empty_get_cachedir