ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
14.89k stars 2.44k forks source link

KeyError: 'Earnings Date' for Ticker.get_earnings_dates #2123

Closed xlsi closed 1 week ago

xlsi commented 1 week ago

Describe bug

.get_earnings_dates not working with both yfinance==0.2.33 and 0.2.49

However, other data could be retrieved as shown below

stock.news

[{'uuid': '27c10fe2-3346-3f68-ad63-43010e027dc4', 'title': 'Physitrack Plc: Champion Health Plus Secures First Order for Nexa AI-powered Self-Service MSK Platform', 'publisher': 'ACCESSWIRE', 'link': 'https://finance.yahoo.com/news/physitrack-plc-champion-health-plus-072000728.html', 'providerPublishTime': 1731396000, 'type': 'STORY', 'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/u0lgIr_kf5YneuW2JNvEzw--~B/aD00MDA7dz00MDA7YXBwaWQ9eXRhY2h5b24-/https://media.zenfs.com/en/accesswire.ca/ae04e40b00c092c6be89a36051c6d473', 'width': 400, 'height': 400, 'tag': 'original'}, {'url': 'https://s.yimg.com/uu/api/res/1.2/vkc0LQXAhlQU33CQOX4Sjw--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/accesswire.ca/ae04e40b00c092c6be89a36051c6d473', 'width': 140, 'height': 140, 'tag': '140x140'}]}, 'relatedTickers': ['PTRK.ST']}, {'uuid': 'd5aa852b-212d-3faa-bd83-c7d771d8bf30', 'title': 'Physitrack PLC – Interim Report: January – September 2024', 'publisher': 'ACCESSWIRE', 'link': 'https://finance.yahoo.com/news/physitrack-plc-interim-report-january-072000850.html', 'providerPublishTime': 1731396000, 'type': 'STORY', 'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/u0lgIr_kf5YneuW2JNvEzw--~B/aD00MDA7dz00MDA7YXBwaWQ9eXRhY2h5b24-/https://media.zenfs.com/en/accesswire.ca/ae04e40b00c092c6be89a36051c6d473', 'width': 400, 'height': 400, 'tag': 'original'}, ... 'tag': 'original'}, {'url': 'https://s.yimg.com/uu/api/res/1.2/vAl04Iq6DYdeYwq_tVkcXw--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/reuters-finance.com/d6f27ee72324f3efae4a6f6e02b0c37d', 'width': 140, 'height': 140, 'tag': '140x140'}]}}]

Simple code that reproduces your problem

import requests import yfinance as yf

session = requests.Session() session.headers.update({"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}) session.verify = False

EARNINGS_CALENDER = ["D05.SI", "O39.SI"]

for ticker in EARNINGS_CALENDER: print(f'Processing {ticker}') stock = yf.Ticker(ticker, session=session) earnings_calendar = stock.get_earnings_dates(limit=30).index

Debug log


KeyError Traceback (most recent call last) File c:\Users\LiangD\AppData\Local\anaconda3\envs\forecast\lib\site-packages\pandas\core\indexes\base.py:3791, in Index.get_loc(self, key) 3790 try: -> 3791 return self._engine.get_loc(casted_key) 3792 except KeyError as err:

File index.pyx:152, in pandas._libs.index.IndexEngine.get_loc()

File index.pyx:181, in pandas._libs.index.IndexEngine.get_loc()

File pandas_libs\hashtable_class_helper.pxi:7080, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas_libs\hashtable_class_helper.pxi:7088, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Earnings Date'

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

KeyError Traceback (most recent call last) Cell In[11], line 11 9 print(f'Processing {ticker}') 10 stock = yf.Ticker(ticker, session=session) ---> 11 earnings_calendar = stock.get_earnings_dates(limit=30).index 12 df_future_cov[f'earningcalender{ticker}'] = np.where(df_future_cov.index.isin(earnings_calendar), 1, 0)

File c:\Users\LiangD\AppData\Local\anaconda3\envs\forecast\lib\site-packages\yfinance\utils.py:108, in log_indent_decorator..wrapper(*args, *kwargs) 105 logger.debug(f'Entering {func.name}()') 107 with IndentationContext(): --> 108 result = func(args, **kwargs) 110 logger.debug(f'Exiting {func.name}()') 111 return result

File c:\Users\LiangD\AppData\Local\anaconda3\envs\forecast\lib\site-packages\yfinance\base.py:2135, in TickerBase.get_earnings_dates(self, limit, proxy) 2133 cn = "Earnings Date" 2134 # - remove AM/PM and timezone from date string -> 2135 tzinfo = dates[cn].str.extract('([AP]M[a-zA-Z])$') 2136 dates[cn] = dates[cn].replace(' [AP]M[a-zA-Z]$', '', regex=True) 2137 # - split AM/PM from timezone

File c:\Users\LiangD\AppData\Local\anaconda3\envs\forecast\lib\site-packages\pandas\core\frame.py:3893, in DataFrame.getitem(self, key) 3891 if self.columns.nlevels > 1: 3892 return self._getitem_multilevel(key) -> 3893 indexer = self.columns.get_loc(key) 3894 if is_integer(indexer): 3895 indexer = [indexer]

File c:\Users\LiangD\AppData\Local\anaconda3\envs\forecast\lib\site-packages\pandas\core\indexes\base.py:3798, in Index.get_loc(self, key) 3793 if isinstance(casted_key, slice) or ( 3794 isinstance(casted_key, abc.Iterable) 3795 and any(isinstance(x, slice) for x in casted_key) 3796 ): 3797 raise InvalidIndexError(key) -> 3798 raise KeyError(key) from err 3799 except TypeError: 3800 # If we have a listlike key, _check_indexing_error will raise 3801 # InvalidIndexError. Otherwise we fall through and re-raise 3802 # the TypeError. 3803 self._check_indexing_error(key)

KeyError: 'Earnings Date'

Bad data proof

No response

yfinance version

0.2.33

Python version

No response

Operating system

No response

ValueRaider commented 1 week ago

Duplicate of #1932

rvimalakempegowda commented 1 day ago

Even I have the same issue!! Please let me know if you have fixed the issue. Enter the company's ticker symbol: AMD Traceback (most recent call last): File "C:\Users\Revanth\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc return self._engine.get_loc(casted_key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Earnings Date'

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

Traceback (most recent call last): File "C:\Users\Revanth\Desktop\Final Project\Earnigns_date\earnings.py", line 57, in earnings_dates = get_earnings_dates(ticker) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Revanth\Desktop\Final Project\Earnigns_date\earnings.py", line 28, in get_earnings_dates earnings_dates = stock.get_earnings_dates(limit=10) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Revanth\AppData\Local\Programs\Python\Python311\Lib\site-packages\yfinance\utils.py", line 104, in wrapper result = func(*args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Revanth\AppData\Local\Programs\Python\Python311\Lib\site-packages\yfinance\base.py", line 634, in get_earnings_dates tzinfo = dates[cn].str.extract('([AP]M[a-zA-Z])$')


  File "C:\Users\Revanth\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 4090, in __getitem__
    indexer = self.columns.get_loc(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Revanth\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc
    raise KeyError(key) from err
KeyError: 'Earnings Date'