我知道很多人问过关于"NotImplementedError“的问题。不过,在翻阅了现有的答案后,我仍然不明白如何在我的情况下解决这个问题。
我的目标是在过去10年中获得平均GDP的前15个国家
Newdata3
数据文件如下所示
创建了一个新的列
Newdata3['avgGDP']=(Newdata3['2006']+Newdata3['2007']+Newdata3['2008']+Newdata3['2009']+Newdata3['2010']+Newdata3['2011']+Newdata3['2012']+Newdata3['2013']+Newdata3['2014']+Newdata3['2015'])/10
按降序排序
Newdata3 = Newdata3.sort_values(by=['avgGDP'], ascending=False)
将Pandas DataFrame转换成一个系列
ans = Newdata3['avgGDP']
ans2 = ans.squeeze()
写下了答案
def answer_three():
ans2= ans.squeeze()
return ans2
raise NotImplementedError()
,然后我收到了错误
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-54-87754d21578b> in <module>
4 return ans2
5
----> 6 raise NotImplementedError()
NotImplementedError:
和这个练习,他们有一个检查要求,我也失败了。但我以为我已经变成了一个系列?
assert type(answer_three()) == pd.Series, "Q3: You should return a Series!"
发布于 2021-05-25 00:06:15
您已经在代码中自己编写了下面的行,从而引发了错误。
raise NotImplementedError()
在执行名为"answer_three“的函数之后,将执行raise NotImplementedError()
,这将引发一个错误。
如果不想引发错误,请删除这一行。
调试代码时使用assert
关键字。assert关键字允许您测试代码匹配中的条件是否返回True,如果不返回,程序将引发AssertionError
assert type(answer_three()) == pd.Series, "Q3: You should return a Series!"
在上面的行中,它检查名为answer_three()
的函数的返回类型是否为pd.series
,然后返回true (什么都不做),否则引发一个AssertionError
https://stackoverflow.com/questions/67683981
复制