diff --git a/README.md b/README.md index 639a340b4..7c16e9810 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,6 @@ Yahoo! finance API is intended for personal use only.** The `Ticker` module, which allows you to access ticker data in a more Pythonic way: -Note: yahoo finance datetimes are received as UTC. - ```python import yfinance as yf @@ -214,6 +212,18 @@ data = yf.download( # or pdr.get_data_yahoo(... ) ``` +### Timezone cache store + +When fetching price data, all dates are localized to stock exchange timezone. +But timezone retrieval is relatively slow, so yfinance attemps to cache them +in your users cache folder. +You can direct cache to use a different location with `set_tz_cache_location()`: +```python +import yfinance as yf +yf.set_tz_cache_location("custom/cache/location") +... +``` + ### Managing Multi-Level Columns The following answer on Stack Overflow is for [How to deal with diff --git a/yfinance/__init__.py b/yfinance/__init__.py index 5e87a98e3..737104531 100644 --- a/yfinance/__init__.py +++ b/yfinance/__init__.py @@ -23,6 +23,7 @@ from .ticker import Ticker from .tickers import Tickers from .multi import download +from .utils import set_tz_cache_location __version__ = version.version __author__ = "Ran Aroussi" @@ -42,4 +43,4 @@ def pdr_override(): pass -__all__ = ['download', 'Ticker', 'Tickers', 'pdr_override'] +__all__ = ['download', 'Ticker', 'Tickers', 'pdr_override', 'set_tz_cache_location'] diff --git a/yfinance/base.py b/yfinance/base.py index 62fcfaa36..a0563417a 100644 --- a/yfinance/base.py +++ b/yfinance/base.py @@ -339,7 +339,11 @@ def _get_ticker_tz(self): if tkr_tz is None: tkr_tz = self.info["exchangeTimezoneName"] # info fetch is relatively slow so cache timezone - utils.cache_store_tkr_tz(self.ticker, tkr_tz) + try: + utils.cache_store_tkr_tz(self.ticker, tkr_tz) + except PermissionError: + # System probably read-only, so cannot cache + pass self._tz = tkr_tz return tkr_tz diff --git a/yfinance/utils.py b/yfinance/utils.py index d611575c3..131c0df66 100644 --- a/yfinance/utils.py +++ b/yfinance/utils.py @@ -316,8 +316,17 @@ def __str__(self): # Simple file cache of ticker->timezone: +_cache_dp = None def get_cache_dirpath(): - return _os.path.join(_ad.user_cache_dir(), "py-yfinance") + if _cache_dp is None: + dp = _os.path.join(_ad.user_cache_dir(), "py-yfinance") + else: + dp = _os.path.join(_cache_dp, "py-yfinance") + return dp +def set_tz_cache_location(dp): + global _cache_dp + _cache_dp = dp + def cache_lookup_tkr_tz(tkr): fp = _os.path.join(get_cache_dirpath(), "tkr-tz.csv") if not _os.path.isfile(fp):