YFINANCE or YAHOOQUERY: Retrieve holdings data

Viewed 277

This question is a follow-up to discussion here, documentation here

how to translate the output of below code into a data frame?

fund = Ticker('hasgx')
fund.fund_holding_info

I started with below,

tickers = ['HACAX']
fund = Ticker(tickers)
fund = fund.fund_holding_info
fund = pd.DataFrame(fund).reset_index().rename(columns={'index': 'Header'})

 fund = fund[fund["Header"] == "holdings"]

with output as,

enter image description here

How can all of this converted as a data frame with rows and columns for 'Holdings'?

1 Answers

The returned information is in the form of a dictionary, so you can specify the items you want to know and bring them into the data frame to get the expected format.

import yahooquery as yq

fund = yq.Ticker('hasgx')
fund.fund_holding_info.keys()
#dict_keys(['hasgx'])

import pandas as pd
df = pd.DataFrame(fund.fund_holding_info['hasgx']['holdings'])

df
    symbol  holdingName     holdingPercent
0   WMS     Advanced Drainage Systems Inc   0.0257
1   FCNCA   First Citizens BancShares Inc Class A   0.0231
2   TPTX    Turning Point Therapeutics Inc  0.0230
3   HXL     Hexcel Corp     0.0222
4   EYE     National Vision Holdings Inc    0.0220
5   SIMO    Silicon Motion Technology Corp ADR  0.0208
6   ATKR    Atkore Inc  0.0202
7   PCRX    Pacira BioSciences Inc  0.0200
8   TKR     The Timken Co   0.0197
9   XLRN    Acceleron Pharma Inc    0.0195

How to make a data frame for each stock unit

import yahooquery as yq
import pandas as pd

stocks = ['hasgx','vinix']

for s in stocks:
  fund = yq.Ticker(s)
  locals()[s] = pd.DataFrame(fund.fund_holding_info[s]['holdings'])

Store them in the same data frame

import yahooquery as yq
import pandas as pd

stocks = ['hasgx','vinix']

df = pd.DataFrame()
for s in stocks:
  fund = yq.Ticker(s)
  tmp = pd.DataFrame(fund.fund_holding_info[s]['holdings'])
  tmp['ticker'] = s
  df = df.append(tmp, ignor_index=True)

df = df[['ticker', 'symbol', 'holdingName', 'holdingPercent']]

    ticker  symbol  holdingName     holdingPercent
0   hasgx   WMS     Advanced Drainage Systems Inc   0.0257
1   hasgx   FCNCA   First Citizens BancShares Inc Class A   0.0231
2   hasgx   TPTX    Turning Point Therapeutics Inc  0.0230
3   hasgx   HXL     Hexcel Corp     0.0222
4   hasgx   EYE     National Vision Holdings Inc    0.0220
5   hasgx   SIMO    Silicon Motion Technology Corp ADR  0.0208
6   hasgx   ATKR    Atkore Inc  0.0202
7   hasgx   PCRX    Pacira BioSciences Inc  0.0200
8   hasgx   TKR     The Timken Co   0.0197
9   hasgx   XLRN    Acceleron Pharma Inc    0.0195
10  vinix   AAPL    Apple Inc   0.0592
11  vinix   MSFT    Microsoft Corp  0.0562
12  vinix   AMZN    Amazon.com Inc  0.0406
13  vinix   FB  Facebook Inc Class A    0.0229
14  vinix   GOOGL   Alphabet Inc Class A    0.0202
15  vinix   GOOG    Alphabet Inc Class C    0.0197
16  vinix   BRK.B   Berkshire Hathaway Inc Class B  0.0145
17  vinix   TSLA    Tesla Inc   0.0144
18  vinix   NVDA    NVIDIA Corp     0.0137
19  vinix   JPM     JPMorgan Chase & Co     0.0130
Related