Skip to content

Commit

Permalink
Some fixes and better debug if failing to fetch timezone from ticker.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Corneliusson committed Oct 25, 2022
1 parent f5973b2 commit 3cee66d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ requests>=2.26
multitasking>=0.0.7
lxml>=4.5.1
appdirs>=1.4.4
pytz>=2022.5
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
packages=find_packages(exclude=['contrib', 'docs', 'tests', 'examples']),
install_requires=['pandas>=0.24.0', 'numpy>=1.15',
'requests>=2.26', 'multitasking>=0.0.7',
'lxml>=4.5.1', 'appdirs>=1.4.4'],
'lxml>=4.5.1', 'appdirs>=1.4.4', 'pytz>=2022.5'],
entry_points={
'console_scripts': [
'sample=sample:main',
Expand Down
18 changes: 13 additions & 5 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ def _fix_unit_mixups(self, df, interval, tz_exchange):

if (median==0).any():
raise Exception("median contains zeroes, why?")
ratio = _np.zeros(median.shape)
ratio = df[data_cols].values/median
# ratio_rounded = (ratio/5).round()*5 # round ratio to nearest 5
ratio_rounded = (ratio/10).round()*10 # round ratio to nearest 10
Expand All @@ -416,7 +415,7 @@ def _fix_unit_mixups(self, df, interval, tz_exchange):
dc = data_cols[j]
for i in _np.where(fj)[0]:
idx = df.index[i]
if not idx in mixups:
if idx not in mixups:
mixups[idx] = {"data":df.loc[idx,data_cols], "fields":set([dc])}
else:
mixups[idx]["fields"].add(dc)
Expand Down Expand Up @@ -537,7 +536,7 @@ def _fix_unit_mixups(self, df, interval, tz_exchange):


def _get_ticker_tz(self, debug_mode, proxy, timeout):
if not self._tz is None:
if self._tz is not None:
return self._tz
cache = utils.get_tz_cache()
tz = cache.lookup(self.ticker)
Expand Down Expand Up @@ -578,7 +577,16 @@ def _fetch_ticker_tz(self, debug_mode, proxy, timeout):
try:
data = session.get(url=url, params=params, proxies=proxy, headers=utils.user_agent_headers, timeout=timeout)
data = data.json()
return data["chart"]["result"][0]["meta"]["exchangeTimezoneName"]
try:
return data["chart"]["result"][0]["meta"]["exchangeTimezoneName"]
except TypeError:
if debug_mode:
print("Could not get exchangeTimezoneName for ticker '{}' reason: {}".format(self.ticker,))
print("Got response: ")
print("-------------")
print(" {}".format(data))
print("-------------")
return None
except Exception as e:
if debug_mode:
print("Failed to get ticker '{}' reason: {}".format(self.ticker, e))
Expand Down Expand Up @@ -1164,7 +1172,7 @@ def get_earnings_dates(self, proxy=None):
dates[cn] = _pd.to_datetime(dates[cn], format="%b %d, %Y, %I %p")
# - instead of attempting decoding of ambiguous timezone abbreviation, just use 'info':
dates[cn] = dates[cn].dt.tz_localize(
tz=self.info["exchangeTimezoneName"])
tz=self.get_info()["exchangeTimezoneName"])

dates = dates.set_index("Earnings Date")

Expand Down
3 changes: 2 additions & 1 deletion yfinance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def safe_merge_dfs(df_main, df_sub, interval):
raise Exception("No data to merge")

df_sub_backup = df_sub.copy()
data_cols = [c for c in df_sub.columns if not c in df_main]
data_cols = [c for c in df_sub.columns if c not in df_main]
if len(data_cols) > 1:
raise Exception("Expected 1 data col")
data_col = data_cols[0]
Expand Down Expand Up @@ -460,6 +460,7 @@ def fix_Yahoo_dst_issue(df, interval):
df.index += _pd.TimedeltaIndex(dst_error_hours, 'h')
return df


def is_valid_timezone(tz: str) -> bool:
try:
_tz.timezone(tz)
Expand Down

0 comments on commit 3cee66d

Please sign in to comment.