YFinance - tickerData.info not working for some stocks

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 07:24:46

问题


import yfinance as yf

#define the ticker symbol
tickerSymbol = "AFT.NZ"

#get data on this ticker
tickerData = yf.Ticker(tickerSymbol)
print(tickerData.info)

This doesn't seem to work. IndexError: list index out of range

Replace "AFT.NZ" with "MSFT" or "FPH.NZ" and it works fine. Going to the Yahoo website, can't see why it wouldn't have data on it.

What's more confusing, is that replacing print(tickerData.info) with tickerDf = tickerData.history(period='max') does print some of the data.

I need the info because I want the full name of the company along with the currency the shares are traded in. Which is why just having the price data isn't the solution.

The AFT.NZ is just an example, most others on the NZX50 seem to have the same problem.


回答1:


There's a merge request from williamsiuhang to fix this that's currently 9 days old. https://github.com/ranaroussi/yfinance/pull/371/commits/7e137357296a1df177399d26543e889848efc021

I just made the change manually myself, go into base.py (in your_py_dir\Lib\site-packages\yfinance) and change line 286:

old line:

self._institutional_holders = holders[1]

new line:

self._institutional_holders = holders[1] if len(holders) > 1 else []



回答2:


I've had the same problem, and see there's a lot of posts on github with the same error.

I've fixed the error with a try & except in the base.py file for yfinance

Line 282

    # holders
    try:
        url = "{}/{}/holders".format(self._scrape_url, self.ticker)
        holders = _pd.read_html(url)
        self._major_holders = holders[0]
        self._institutional_holders = holders[1]

        if 'Date Reported' in self._institutional_holders:
            self._institutional_holders['Date Reported'] = _pd.to_datetime(
                self._institutional_holders['Date Reported'])
        if '% Out' in self._institutional_holders:
            self._institutional_holders['% Out'] = self._institutional_holders[
            '   % Out'].str.replace('%', '').astype(float)/100
    except:
        print("institutional_holders error")

Not a great solution, but makes it run for me. I'm not a great programmer, so I hope the problem get fixed in a more delicate way by the developer(s).



来源:https://stackoverflow.com/questions/60469752/yfinance-tickerdata-info-not-working-for-some-stocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!