Skip to content
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

Cannot retrieve price from downloaded data if at least one symbol is invalid #1743

Closed
adriengivry opened this issue Nov 17, 2023 · 4 comments

Comments

@adriengivry
Copy link

Describe bug

When using yf.download with multiple symbols, it appears that if at least one symbol in the list is delisted/not available, the date isn't accessible anymore

Simple code that reproduces your problem

import yfinance as yf

foo = yf.download(["AAPL", "INTC"], start='2023-11-16', end='2023-11-17')
bar = yf.download(["AAPL", "ATVI"], start='2023-11-16', end='2023-11-17')

print(foo['Close']['AAPL']) # <-- The date appear properly in the dataframe: 2023-11-16
print(bar['Close']['AAPL']) # <-- The date appear with a different format: 2023-11-16 00:00:00

print(foo['Close']['AAPL']['2023-11-16']) # <-- Outputs "189.7100067138672"
print(bar['Close']['AAPL']['2023-11-16']) # <-- KeyError: '2023-11-16'

Debug log

[*********************100%%**********************]  2 of 2 completed
[*********************100%%**********************]  2 of 2 completed

1 Failed download:
['ATVI']: Exception('%ticker%: No price data found, symbol may be delisted (1d 2023-11-16 -> 2023-11-17)')
Date
2023-11-16    189.710007
Name: AAPL, dtype: float64
Date
2023-11-16 00:00:00    189.710007
Name: AAPL, dtype: float64
189.7100067138672
Traceback (most recent call last):
  File "C:\Users\adrie\scoop\apps\python310\current\lib\site-packages\pandas\core\indexes\base.py", line 3790, in get_loc
    return self._engine.get_loc(casted_key)
  File "index.pyx", line 152, in pandas._libs.index.IndexEngine.get_loc
  File "index.pyx", line 181, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '2023-11-16'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\adrie\test.py", line 18, in <module>
    print(bar['Close']['AAPL']['2023-11-16'])
  File "C:\Users\adrie\scoop\apps\python310\current\lib\site-packages\pandas\core\series.py", line 1040, in __getitem__
    return self._get_value(key)
  File "C:\Users\adrie\scoop\apps\python310\current\lib\site-packages\pandas\core\series.py", line 1156, in _get_value
    loc = self.index.get_loc(label)
  File "C:\Users\adrie\scoop\apps\python310\current\lib\site-packages\pandas\core\indexes\base.py", line 3797, in get_loc
    raise KeyError(key) from err
KeyError: '2023-11-16'

Bad data proof

No response

yfinance version

0.2.31

Python version

3.10.5

Operating system

Windows 11 Pro (22H2) 22621.2715

@adriengivry
Copy link
Author

Using Timestamp('2023-11-16') as the key works, but using 2023-11-16 doesn't (only happens when at least one symbol is not available)

foo = yf.download(["AAPL", "INTC"], start='2023-11-16', end='2023-11-17') # <-- Both AAPL and INTC being valid symbols
bar = yf.download(["AAPL", "ATVI"], start='2023-11-16', end='2023-11-17') # <-- ATVI being an invalid symbol (delisted)

print(foo['Close']['AAPL']['2023-11-16']) # <-- Outputs "189.7100067138672"
print(bar['Close']['AAPL']['2023-11-16']) # <-- KeyError: '2023-11-16'
print(bar['Close']['AAPL'][Timestamp('2023-11-16')]) # <-- Outputs "189.7100067138672"

@chotaGit
Copy link

I have seen this happen with pre-market data for GS. As a work around, put a try/except around the yf.download call.

@adriengivry
Copy link
Author

@chotaGit my previous comment shows that it's still possible to retrieve the price value of symbol at a certain date ("AAPL" in my example) when another symbol is missing ("ATVI" in my example). However the date required key to access the price value needs to be specified explicitly using pandas.Timestamp. Catching the exception wouldn't help getting the price value.

@ValueRaider
Copy link
Collaborator

FYI this is a simple one-line fix, so I'll leave this as an example for #1084

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants