user6515293
user6515293

Reputation: 41

How to compare dividend yields to S&P 500 with yfinance?

I would like to compare the dividend yields of a list of ticker symbols to that of the S&P 500. I have a list of ticker symbols that includes SPY for the S&P 500. When I try and print the trailing annual dividend yield I get "None" returned for SPY though many of the other symbols return yields.

Am I doing something wrong? Is there another way to get the dividend yields (forward or trailing annual) for a large list of stocks? (I noticed this code is slow to run. It takes about 4 seconds per ticker symbol to pull down the dividend yield data.)

My list of ticker symbols is:

['A', 'ABC', 'ADI', 'AEP', 'AFL', 'ALGN', 'AME', 'AMT', 'ANSS', 'APH', 'APTV', 'ARE', 'AZN', 'CAT', 'CCI', 'CNC', 'CNP', 'COST', 'CSX', 'CTAS', 'CTSH', 'DHI', 'DISCA', 'DISCK', 'DOV', 'DTE', 'ED', 'EFX', 'EMR', 'EOG', 'EQIX', 'EQR', 'ETR', 'EW', 'FTI', 'FTV', 'GPN', 'GRMN', 'HAL', 'HON', 'HPE', 'HSIC', 'IFF', 'ILMN', 'IP', 'IQV', 'IT', 'ITW', 'JBHT', 'JCI', 'KEYS', 'LNG', 'LNT', 'LOW', 'MHK', 'MKC', 'MLM', 'MSCI', 'NEE', 'NEM', 'NI', 'NOV', 'NTAP', 'NUE', 'NVDA', 'NXPI', 'O', 'PANW', 'PAYX', 'PCAR', 'PH', 'PLD', 'PXD', 'QQQ', 'REGN', 'RF', 'ROST', 'RSG', 'SBAC', 'SCCO', 'SIVB', 'SLB', 'SNPS', 'SPG', 'SPY', 'SSNC', 'SWKS', 'SYY', 'TFX', 'UNP', 'URI', 'VMC', 'VRSK', 'VRTX', 'WAT', 'WEC', 'WM', 'WPC', 'WRK', 'XLNX', 'ZTS']

Pertinent section of code.

SPYindex = tickerslst.index("SPY")

for stock in tickerslst:
    info = yf.Ticker(stock).info
    divlst.append(info.get('trailingAnnualDividendYield'))
    print(str(tickerslst.index(stock)) + " out of " + str(len(tickerslst)))

print(divlst[SPYindex])
print(divlst)

This is what is returned from the print statements.

None
[0.0046096845, 0.01438488, 0.015404515, 0.032110605, 0.020433111, None, 0.005392014, 0.016577125, None, 0.007298284, 0.0013338993, 0.020924576, 0.047879618, 0.018849796, 0.025978027, None, 0.027509294, 0.006252512, 0.03129613, 0.010939627, 0.011593456, 0.0076702805, None, None, 0.011506642, 0.03488778, 0.039554927, 0.0059976927, 0.019375362, 0.022058824, 0.013327764, 0.029155577, 0.03421915, None, 0, 0.0036861508, 0.004550493, 0.01599809, 0.008941879, 0.0157922, 0.032042723, None, 0.01939394, None, 0.034616683, None, None, 0.01926956, 0.006206936, 0.014419115, None, None, 0.025249714, 0.012335311, None, 0.014651592, 0.00581154, 0.0047934987, 0.017195925, 0.029741673, 0.033765215, 0, 0.023334954, 0.012800191, 0.0031702, 0.00784168, 0.039013203, None, 0.021165827, 0.015775204, 0.012013651, 0.017957266, 0.014577836, 0.0044195, None, 0.030243903, 0.0045724367, 0.013933814, 0.0059071253, 0.025293857, None, 0.017636685, None, 0.038901772, None, 0.007318653, 0.01061095, 0.022658609, 0.003715035, 0.017032485, None, 0.0073238844, 0.005751935, None, None, 0.02676437, 0.0146736605, 0.05316713, 0.02079266, 0.0077746706, 0.0042187814]

Upvotes: 0

Views: 1525

Answers (0)

Related Questions