Home > Enterprise >  how to access and manipulate rows from pandas dataframe in yahoo finance api
how to access and manipulate rows from pandas dataframe in yahoo finance api

Time:08-21

I would like to use the yahoo finance API for my website, as it has the most thorough and complete information. I am googling how to work with pandas dataframes and nothing is working to access the information in row one to be able to save this to my database and display it on my site:

ipdb> yahoo_stock_obj.earnings_history
   Symbol               Company           Earnings Date  EPS Estimate  Reported EPS Surprise(%)
0      HD  The Home Depot, Inc.   Aug 16, 2022, 2 AMEDT          4.94          5.05        2.14
1      HD  The Home Depot, Inc.   May 17, 2022, 2 AMEDT          3.68          4.09       11.26
2      HD  The Home Depot, Inc.   Feb 22, 2022, 1 AMEST          3.18          3.21        0.98
3      HD  The Home Depot, Inc.  Nov 16, 2021, 12 AMEST          3.40          3.92       15.26
4      HD  The Home Depot, Inc.   Aug 17, 2021, 2 AMEDT          4.44          4.53        1.94
..    ...                   ...                     ...           ...           ...         ...
95     HD  The Home Depot, Inc.  Nov 17, 1998, 12 AMEST          0.17          0.17        3.25
96     HD  The Home Depot, Inc.  Aug 18, 1998, 12 AMEDT          0.20          0.21        3.26
97     HD  The Home Depot, Inc.  May 19, 1998, 12 AMEDT          0.14          0.15        7.14
98     HD  The Home Depot, Inc.  Feb 24, 1998, 12 AMEST          0.14          0.14       -0.15
99     HD  The Home Depot, Inc.  Nov 18, 1997, 12 AMEST          0.13          0.13        3.64

[100 rows x 6 columns]
ipdb> yahoo_stock_obj.earnings_history[0][0]
*** ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

ipdb> yahoo_stock_obj.earnings_history.iteritems()
*** ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

ipdb> yahoo_stock_obj.earnings_history.columns
*** ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

ipdb> yahoo_stock_obj.earnings_history.columns()
*** ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

ipdb> yahoo_stock_obj.earnings_history.bool()
*** ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

ipdb> dataframe = yahoo_stock_obj.earnings_history
*** ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

ipdb> dataframe = yahoo_stock_obj.get_earnings_history
ipdb> dataframe = yahoo_stock_obj.get_earnings_history()
*** ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
ipdb> dataframe = yahoo_stock_obj.get_earnings_history
ipdb> dataframe
<bound method TickerBase.get_earnings_history of yfinance.Ticker object <HD>>

ipdb> dataframe()
*** ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

ipdb> yahoo_stock_obj.earnings_history.iloc[0]
*** ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

the script:

import yfinance

yahoo_stock_obj = yfinance.Ticker('HD'.upper())

info = yahoo_stock_obj.get_info()

import ipdb; ipdb.set_trace()

how can I iterate over these rows and extract data for the date, eps estimate, reported eps, and surprise%?

CodePudding user response:

I found that there is inner mistake in module.

It checks if data is already downloaded - if self._earnings_history: - and when you run it first time then self._earnings_history has value None and this works. But when you run it second time then self._earnings_history has already DataFrame with many values and it doesn't know what value it has to return in if/else. It would need if self._earnings_history is not None:


At this moment you have to assign result to variable and use only this variable.

import yfinance

yahoo_stock_obj = yfinance.Ticker('HD')

history = yahoo_stock_obj.earnings_history

print(history)

print(history.loc[0])

print(history["Symbol"])

BTW:

I already send this problem to author(s):

Second execution of .earnings_history gives error because code doesn't use is not None #1051

  • Related