New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance of fetching Ticker timezone #1112
Conversation
@fredrik-corneliusson - just giving you an opportunity to review in case you disagree anywhere |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just had a few suggestions.
yfinance/base.py
Outdated
def _fetch_ticker_tz(self, proxy=None, timeout=None): | ||
# Query Yahoo for basic price data just to get returned timezone | ||
|
||
params = {"range":"1wk", "interval":"1d"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think range can be "1d" as well to get even less data.
yfinance/base.py
Outdated
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"] | ||
except: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe if debug is True print the exception
example:
except Exception as e:
if debug_mode:
print("Failed to get ticker '{}' reason: {}".format(self.ticker, e))
yfinance/base.py
Outdated
@@ -533,7 +533,7 @@ def _fix_unit_mixups(self, df, interval, tz_exchange): | |||
return df | |||
|
|||
|
|||
def _get_ticker_tz(self): | |||
def _get_ticker_tz(self, proxy=None, timeout=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is _get_ticker_tz called without proxy and timeout in any other place?
As proxy and timeout is passed to the history method and not Ticker constructor, any calls to this from another methods might not have the correct proxy. (However I do not fully understand the use of proxies in yfinance so this might not be an issue).
Also if timeout is None the request will hang indefinitely, so maybe default to something reasonable.
https://requests.readthedocs.io/en/latest/user/quickstart/#timeouts
Maybe make sure any new code do not call it without setting proxy and timeout so do not make them optional. And if timeout is set to None use a default timeout .
@fredrik-corneliusson All good suggestions. |
Instead of fetching and building
info
, just fetch basic price data which contains timezone. ~1.7x speedup. Hopefully also reduces failures to get timezone (issue #1076)