简体   繁体   English

在 Jupyter 中调用定义的函数时命名错误

[英]Name error when calling defined function in Jupyter

I am following a tutorial over at https://blog.patricktriest.com/analyzing-cryptocurrencies-python/ and I've got a bit stuck.我正在关注https://blog.patricktriest.com/analyzing-cryptocurrencies-python/ 上的教程,但我有点卡住了。 I am tyring to define, then immediately call, a function.我很想定义一个函数,然后立即调用。

My code is as follows:我的代码如下:

def merge_dfs_on_column(dataframes, labels, col):
    '''merge a single column of each dataframe on to a new combined dataframe'''
    series_dict={}
    for index in range(len(dataframes)):
        series_dict[labels[index]]=dataframes[index][col]
    return pd.DataFrame(series_dict)
# Merge the BTC price dataseries into a single dataframe
btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')

I can clearly see that I have defined the merge_dfs_on_column fucntion and I think the syntax is correct, however, when I call the function on the last line, I get the following error:我可以清楚地看到我已经定义了 merge_dfs_on_column 功能,并且我认为语法是正确的,但是,当我在最后一行调用该函数时,出现以下错误:

NameError                                 Traceback (most recent call last)
<ipython-input-22-a113142205e3> in <module>()
      1 # Merge the BTC price dataseries into a single dataframe
----> 2 btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')

NameError: name 'merge_dfs_on_column' is not defined

I have Googled for answers and carefully checked the syntax, but I can't see why that function isn't recognised when called.我在谷歌上搜索了答案并仔细检查了语法,但我不明白为什么在调用时无法识别该函数。

Your function definition isn't getting executed by the Python interpreter before you call the function.在调用函数之前,Python 解释器不会执行您的函数定义。

Double check what is getting executed and when.仔细检查正在执行的内容和时间。 In Jupyter it's possible to run code out of input-order, which seems to be what you are accidentally doing.在 Jupyter 中,可以不按输入顺序运行代码,这似乎是您不小心在做的事情。 (perhaps try 'Run All') (也许尝试“全部运行”)

Well, if you're defining yourself,好吧,如果你要定义自己,

Then you probably have copy and pasted it directly from somewhere on the web and it might have characters that you are probably not able to see.然后,您可能直接从网络上的某个地方复制并粘贴了它,并且其中可能包含您可能看不到的字符。

Just define that function by typing it and use pass and comment out other code and see if it is working or not.只需通过键入它来定义该函数,然后使用pass并注释掉其他代码,看看它是否有效。

"run all" does not work. “全部运行”不起作用。
Shutting down the kernel and restarting does not help either.关闭内核并重新启动也无济于事。

If I write:如果我写:

def whatever(a):
    return a*2

whatever("hallo")

in the next cell, this works.在下一个单元格中,这是有效的。

I have also experienced this kind of problem frequently in jupyter notebook我在jupyter notebook中也经常遇到这种问题
But after replacing %% with %%time the error resolved.但是在用%% %%time替换%%后,错误解决了。 I didn't know why?我不知道为什么?
So,after some browsing i get that this is not jupyter notenook issue,it is ipython issue因此,经过一些浏览后,我发现这不是 jupyter noteook 问题,而是 ipython 问题
and here is the issue and also this problem is answered in this stackoverflow question这是问题,这个问题也在这个stackoverflow question中得到了回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM