未实现错误:回溯(最近一次调用):<module>

2024-09-28 22:04:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道很多人问过关于“未实现错误”的问题。然而,在我查看了现有的答案后,我仍然不明白如何在我的情况下解决它

我的目标是获得过去10年平均GDP排名前15位的国家

Newdata3数据帧如下所示 enter image description here

创建了一个新列

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)

已将数据帧转换为系列

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!"

enter image description here


Tags: 数据答案answerreturn错误情况seriesthree
1条回答
网友
1楼 · 发布于 2024-09-28 22:04:51

您自己在代码中通过编写以下行引发了错误

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

相关问题 更多 >