英文:
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
- deleting it
- googling it
- restarting kernel
- restarting anaconda
- 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"]
为了调试,您还可以尝试在错误发生之前打印sp500
或sp500.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论