Skip to content

Commit

Permalink
Backport ticker tz verification for nice error
Browse files Browse the repository at this point in the history
  • Loading branch information
ValueRaider committed Oct 31, 2022
1 parent e99e61f commit 23b6ad1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
32 changes: 21 additions & 11 deletions yfinance/base.py
Expand Up @@ -150,17 +150,14 @@ def history(self, period="1mo", interval="1d",

if start or period is None or period.lower() == "max":
# Check can get TZ. Fail => probably delisted
try:
tz = self._get_ticker_tz(debug_mode, proxy, timeout)
except KeyError as e:
if "exchangeTimezoneName" in str(e):
shared._DFS[self.ticker] = utils.empty_df()
shared._ERRORS[self.ticker] = err_msg
if "many" not in kwargs and debug_mode:
print('- %s: %s' % (self.ticker, err_msg))
return utils.empty_df()
else:
raise
tz = self._get_ticker_tz(debug_mode, proxy, timeout)
if tz is None:
# Every valid ticker has a timezone. Missing = problem
shared._DFS[self.ticker] = utils.empty_df()
shared._ERRORS[self.ticker] = err_msg
if "many" not in kwargs and debug_mode:
print('- %s: %s' % (self.ticker, err_msg))
return utils.empty_df()

if end is None:
end = int(_time.time())
Expand Down Expand Up @@ -336,6 +333,19 @@ def _get_ticker_tz(self, debug_mode, proxy, timeout):
return self._tz

tkr_tz = utils.cache_lookup_tkr_tz(self.ticker)

if tkr_tz is not None:
invalid_value = isinstance(tkr_tz, str)
if not invalid_value:
try:
_tz.timezone(tz)
except:
invalid_value = True
if invalid_value:
# Clear from cache and force re-fetch
utils.cache_store_tkr_tz(self.ticker, None)
tkr_tz = None

if tkr_tz is None:
tkr_tz = self._fetch_ticker_tz(debug_mode, proxy, timeout)

Expand Down
39 changes: 21 additions & 18 deletions yfinance/utils.py
Expand Up @@ -336,32 +336,35 @@ def cache_lookup_tkr_tz(tkr):
return None

mutex.acquire()
df = _pd.read_csv(fp)
df = _pd.read_csv(fp, index_col="Ticker")
mutex.release()
f = df["Ticker"] == tkr
if sum(f) == 0:
if tkr in df.index:
return df.loc[tkr,"Tz"]
else:
return None

return df["Tz"][f].iloc[0]
def cache_store_tkr_tz(tkr,tz):
df = _pd.DataFrame({"Ticker":[tkr], "Tz":[tz]})

dp = get_cache_dirpath()
fp = _os.path.join(dp, "tkr-tz.csv")
mutex.acquire()
if not _os.path.isdir(dp):
_os.makedirs(dp)
fp = _os.path.join(dp, "tkr-tz.csv")
if not _os.path.isfile(fp):
df.to_csv(fp, index=False)
mutex.release()
return

df_all = _pd.read_csv(fp)
f = df_all["Ticker"]==tkr
if sum(f) > 0:
mutex.release()
raise Exception("Tkr {} tz already in cache".format(tkr))
if (not _os.path.isfile(fp)) and (tz is not None):
df = _pd.DataFrame({"Tz":[tz]}, index=[tkr])
df.index.name = "Ticker"
df.to_csv(fp)

_pd.concat([df_all,df]).to_csv(fp, index=False)
else:
df = _pd.read_csv(fp, index_col="Ticker")
if tz is None:
# Delete if in cache:
if tkr in df.index:
df.drop(tkr).to_csv(fp)
else:
if tkr in df.index:
raise Exception("Tkr {} tz already in cache".format(tkr))
df.loc[tkr,"Tz"] = tz
df.to_csv(fp)

mutex.release()

0 comments on commit 23b6ad1

Please sign in to comment.