pandas 未定义全局名称“inf”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26401647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:35:13  来源:igfitidea点击:

global name 'inf' is not defined

pythonpandasipython

提问by BMichell

I have developed a function that was on its own in a script. I ran that script to place the function in my namespace. The line of code I have that was working fine is:

我在脚本中开发了一个独立的函数。我运行该脚本将函数放在我的命名空间中。我运行良好的代码行是:

df.my_variable.replace([inf, -inf, nan, NaN], 0, inplace=True)

I have now moved this function into mymodule.py. I then try to run it:

我现在已将此功能移至mymodule.py. 然后我尝试运行它:

import mymodule as mm
mm.myfun()

The line of code to replace inf, etc., now throws the following error:

要替换的代码行inf等,现在抛出以下错误:

global name 'inf' is not defined

I'm sure this is a legacy of developing interactively in IPython, but I'm stumped as to how I would define infto make this work.

我确信这是在 IPython 中进行交互式开发的遗产,但我对如何定义inf以使其工作感到困惑。

回答by Darth Kotik

Try this:

尝试这个:

from numpy import inf

回答by greole

infis defined in numpys namespace, so if you just

inf在 numpys 命名空间中定义,所以如果你只是

from numpy import inf

copy a part of the original script you probably forgot to import this from numpy. Pandas internally uses a lot of numpys functionality

复制您可能忘记从 numpy 导入的原始脚本的一部分。Pandas 内部使用了很多 numpys 功能

回答by Lê V? Linh

assume that you have numpyimported as np, you can replace infby np.inf

假设您已numpy导入 as np,则可以替换infnp.inf

回答by Mehdi Aghagolzadeh

No need to tap into numpy, simply:

无需使用 numpy,只需:

inf = float('inf')