KeyError: ‘Dividends’

huangapple go评论106阅读模式
英文:

KeyError: 'Dividends'

问题

del sp500["红利"]
del sp500["股票拆分"]
英文:

for a project I was doing, I wrote

del sp500["Dividends"]
del sp500["Stock Splits"]

this resulted in an error that I could not for the life of me understand. The error was

# there was no match, this call should raise the appropriate

I am using anaconda's JuypterLab on python3

  1. deleting it
  2. googling it
  3. restarting kernel
  4. restarting anaconda
  5. reinstalling yfinance (pretty sure that wasnt the issue)

答案1

得分: 1

一个简单的解决方法是在尝试删除它们之前检查"Dividends""Stock Splits"是否是sp500的键:

if "Dividends" in sp500.keys():
    del sp500["Dividends"]
if "Stock Splits" in sp500.keys():
    del sp500["Stock Splits"]

为了调试,您还可以尝试在错误发生之前打印sp500sp500.keys(),以确保它看起来与您预期的一样。

英文:

A simple workaround would be checking if "Dividends" and "Stock Splits" are keys of sp500 before trying to delete them:

if "Dividends" in sp500.keys():
    del sp500["Dividends"]
if "Stock Splits" in sp500.keys():
    del sp500["Stock Splits"]

For debugging, you could also try to print sp500 or sp500.keys() before the error occurs to make sure it looks like you expected.

huangapple
  • 本文由 发表于 2023年4月20日 00:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定