getting financial data from yahoo finance with yfinance python

6.8k views Asked by At

I was trying the yfinance package trying to get fianacials dataframe. Although, it worked nice getting info,history and actions, it didn't work with financials, balance_sheet and other methods even with different stocks

INPUT

import yfinance as yf
msft = yf.Ticker("MSFT")
print(msft.financials)

OUTPUT

Empty DataFrame
Columns: [Open, High, Low, Close, Adj Close, Volume]
Index: []`

The columns are also wrong. I am searching for a way to fix the problem or another solution to get data.

4

There are 4 answers

4
r-beginners On

As I understand it, msft.financials can be referenced to see what kind of data the stock can get in advance. For example, whether there is an adjusted close or not and how long the data is available. You can check this with msft.history(period='max').

print(msft.history(period="max"))
              Open    High     Low   Close      Volume  Dividends  \
Date                                                                
1986-03-13    0.06    0.06    0.06    0.06  1031788800        0.0   
1986-03-14    0.06    0.07    0.06    0.06   308160000        0.0   
1986-03-17    0.06    0.07    0.06    0.07   133171200        0.0   
1986-03-18    0.07    0.07    0.06    0.06    67766400        0.0   
1986-03-19    0.06    0.06    0.06    0.06    47894400        0.0   
...            ...     ...     ...     ...         ...        ...   
2020-09-25  203.55  209.04  202.54  207.82    29437300        0.0   
2020-09-28  210.88  212.57  208.06  209.44    32004900        0.0   
2020-09-29  209.35  210.07  206.81  207.26    24221900        0.0   
2020-09-30  207.73  211.98  206.54  210.33    33829100        0.0   
2020-10-01  213.49  213.99  211.32  212.46    27115800        0.0  

You can get the actual data as follows.

data = yf.download("MSFT", start="2019-01-01", end="2019-04-30")
data
    Open    High    Low Close   Adj Close   Volume
Date                        
2018-12-31  101.290001  102.400002  100.440002  101.570000  99.300156   33173800
2019-01-02  99.550003   101.750000  98.940002   101.120003  98.860214   35329300
2019-01-03  100.099998  100.190002  97.199997   97.400002   95.223351   42579100
2019-01-04  99.720001   102.510002  98.930000   101.930000  99.652115   44060600
2019-01-07  101.639999  103.269997  100.980003  102.059998  99.779205   35656100
... ... ... ... ... ... ...
2019-04-23  124.099998  125.580002  123.830002  125.440002  123.160469  24025500
2019-04-24  125.790001  125.849998  124.519997  125.010002  122.738281  31257000
2019-04-25  130.059998  131.369995  128.830002  129.149994  126.803055  38033900
2019-04-26  129.699997  130.520004  129.020004  129.889999  127.529594  23654900
2019-04-29  129.899994  130.179993  129.350006  129.770004  127.411781  16324200
0
AnaNewbie69 On

While trying to install yahooquery I get the following error.

ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'C:\Users\xxxx\anaconda3\Lib\site-packages\~xml\etree.cp38-win32.pyd' Consider using the --user option or check the permissions

Your advice would be appreciated.

0
Amir nazary On

An alternative to this issue, is to use yahooquery package for querying financials and balance sheets. Install the package with:

pip install yahooquery

and query the balance sheets like this:

import yahooquery

# balance sheet of Apple
ticker = yahooquery.Ticker('AAPL')
ticker.balance_sheet()
0
Daruri On

This is an already known issue as you can see on this GitHub issue. It is already solved but changes are not released officially yet.

You can see on the "files changed" section the changes that you could apply to your local package code as a quick solution.