pandas “numpy.ndarray”对象不可调用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19348851/
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
'numpy.ndarray' object is not callable error
提问by trinity
Hi I am getting the following error
嗨,我收到以下错误
'numpy.ndarray' object is not callable
“numpy.ndarray”对象不可调用
when performing the calculation in the following manner
按以下方式进行计算时
rolling_means = pd.rolling_mean(prices,20,min_periods=20)`
rolling_std = pd.rolling_std(prices, 20)`
#print rolling_means.head(20)
upper_band = rolling_means + (rolling_std)* 2
lower_band = rolling_means - (rolling_std)* 2
I am not sure how to resolve, can someone point me in right direction....
我不知道如何解决,有人可以指出我正确的方向....
回答by DJG
The error TypeError: 'numpy.ndarray' object is not callable
means that you tried to call a numpy array as a function. We can reproduce the error like so in the repl:
该错误TypeError: 'numpy.ndarray' object is not callable
意味着您尝试将 numpy 数组作为函数调用。我们可以像这样在 repl 中重现错误:
In [16]: import numpy as np
In [17]: np.array([1,2,3])()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/user/<ipython-input-17-1abf8f3c8162> in <module>()
----> 1 np.array([1,2,3])()
TypeError: 'numpy.ndarray' object is not callable
If we are to assume that the error is indeed coming from the snippet of code that you posted (something that you should check,) then you must have reassigned either pd.rolling_mean
or pd.rolling_std
to a numpy array earlier in your code.
如果我们假设错误确实来自您发布的代码片段(您应该检查的内容),那么您必须在代码的前面重新分配pd.rolling_mean
或分配pd.rolling_std
给 numpy 数组。
What I mean is something like this:
我的意思是这样的:
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: pd.rolling_mean(np.array([1,2,3]), 20, min_periods=5) # Works
Out[3]: array([ nan, nan, nan])
In [4]: pd.rolling_mean = np.array([1,2,3])
In [5]: pd.rolling_mean(np.array([1,2,3]), 20, min_periods=5) # Doesn't work anymore...
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/user/<ipython-input-5-f528129299b9> in <module>()
----> 1 pd.rolling_mean(np.array([1,2,3]), 20, min_periods=5) # Doesn't work anymore...
TypeError: 'numpy.ndarray' object is not callable
So, basically you need to search the rest of your codebase for pd.rolling_mean = ...
and/or pd.rolling_std = ...
to see where you may have overwritten them.
因此,基本上您需要搜索代码库的其余部分pd.rolling_mean = ...
和/或pd.rolling_std = ...
查看可能覆盖它们的位置。
另外,如果你愿意,你可以输入
reload(pd)
reload(pd)
就在您的代码段之前,这应该通过将其值恢复为pd
pd
您最初导入的值来运行,但我仍然highly强烈建议您尝试查找可能已重新分配给定功能的位置。