yfinance icon indicating copy to clipboard operation
yfinance copied to clipboard

download-raise-errors: Add raise_errors and raises to download()

Open JTarball opened this issue 9 months ago • 4 comments

download() has no raise_errors flag which seems like an oversight

https://github.com/ranaroussi/yfinance/discussions/1832

I have added 'raise_errors' and 'raises' if you need granular exception raises e.g. only the rate limiting exception

JTarball avatar Apr 11 '25 10:04 JTarball

What happens when two different tickers throw different exceptions?

ValueRaider avatar Apr 12 '25 15:04 ValueRaider

Should raise them separately

In this example test raises=[YFException] which is the base exception .. so all are raised.

Note it will still logger the error also, I assume thats ok.

e.g.

  • YFPricesMissingError**: $EGS745W1C011.CA
  • YFTzMissingError**: $9519.SR:

{"timestamp":"2025-04-13 09:43:12,972","severity":"INFO","name":"test.jobs.prices","message":"-- Processing batch 15 of 3409 --","task_index":"0","task_count":"1","retry_count":"0","component":"shared.envutil"} [**********************84%*************** ] 21 of 25 completedException in thread Thread-399 (_run_via_pool): Traceback (most recent call last): File "/Users/<REDACTED>/.pyenv/versions/3.11.1/lib/python3.11/threading.py", line 1038, in _bootstrap_inner self.run() File "/Users/<REDACTED>/.pyenv/versions/3.11.1/lib/python3.11/threading.py", line 975, in run self._target(*self._args, **self._kwargs) File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/multitasking/__init__.py", line 104, in _run_via_pool return callee(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/multi.py", line 268, in _download_one_threaded _download_one(ticker, start, end, auto_adjust, back_adjust, repair, File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/multi.py", line 297, in _download_one raise exc File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/multi.py", line 284, in _download_one data = Ticker(ticker).history( ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/utils.py", line 104, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/base.py", line 82, in history return self._lazy_load_price_history().history(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/utils.py", line 104, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/scrapers/history.py", line 245, in history raise _exception yfinance.exceptions.**YFPricesMissingError**: $EGS745W1C011.CA: possibly delisted; no price data found (1d 2020-04-13 -> 2025-04-12) [**********************88%***************** ] 22 of 25 completed{"timestamp":"2025-04-13 09:48:21,342","severity":"ERROR","name":"yfinance","message":"Failed to get ticker '9519.SR' reason: 'NoneType' object has no attribute 'name'","component":"shared.envutil"} Exception in thread Thread-393 (_run_via_pool): Traceback (most recent call last): File "/Users/<REDACTED>/.pyenv/versions/3.11.1/lib/python3.11/threading.py", line 1038, in _bootstrap_inner self.run() File "/Users/<REDACTED>/.pyenv/versions/3.11.1/lib/python3.11/threading.py", line 975, in run self._target(*self._args, **self._kwargs) File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/multitasking/__init__.py", line 104, in _run_via_pool return callee(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/multi.py", line 268, in _download_one_threaded _download_one(ticker, start, end, auto_adjust, back_adjust, repair, File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/multi.py", line 297, in _download_one raise exc File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/multi.py", line 284, in _download_one data = Ticker(ticker).history( ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/utils.py", line 104, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/base.py", line 82, in history return self._lazy_load_price_history().history(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/utils.py", line 104, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/<REDACTED>/Library/Caches/pypoetry/virtualenvs/test-pEPzHlFW-py3.11/lib/python3.11/site-packages/yfinance/scrapers/history.py", line 121, in history raise _exception yfinance.exceptions.**YFTzMissingError**: $9519.SR: possibly delisted; no timezone found [*********************100%***********************] 23 of 25 completed {"timestamp":"2025-04-13 09:48:21,366","severity":"ERROR","name":"yfinance","message":"\n2 Failed downloads:","task_index":"0","task_count":"1","retry_count":"0","component":"shared.envutil"} {"timestamp":"2025-04-13 09:48:21,366","severity":"ERROR","name":"yfinance","message":"['EGS745W1C011.CA']: possibly delisted; no price data found (1d 2020-04-13 -> 2025-04-12)","task_index":"0","task_count":"1","retry_count":"0","component":"shared.envutil"}

JTarball avatar Apr 13 '25 08:04 JTarball

@ValueRaider anything else you need from me?

JTarball avatar Apr 16 '25 11:04 JTarball

I've pushed a commit that I think make sense, helps with many errors (imagine 10s-100s). Try it:

yf.download(tkrs, raise_errors=True)
yf.download(tkrs, raise_errors='one')

ValueRaider avatar Apr 16 '25 20:04 ValueRaider