'numpy.ndarray' object is not callable error - pandas

Hi I am getting the following error
'numpy.ndarray' object is not callable
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....

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:
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.
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.
Also, if you'd like, you can put in reload(pd) just before your snippet, which should make it run by restoring the value of pd to what you originally imported it as, but I still highly recommend that you try to find where you may have reassigned the given functions.

For everyone with this problem in 2021, sometimes you can have this problem when you create
a numpy variable with the same name as one of your function, what happens is that instead of calling the function python tries to call the numpy array as a function and you get the error, just change the name of the numpy variable

I met the same question and the solved.
The point is that my function parameters and variables have the same name.
Try to give them different name.

Related

DataFrame object is not callable with AIF360

I encounter TypeError: 'DataFrame' object is not callable with the following. Anyone can help? Thanks.
%cd -
dataset_orig = df_data_1(protected_attribute_names=['Gender'],
privileged_classes=['Male'],
features_to_drop=[])
dataset_orig_train, dataset_orig_test = dataset_orig.split([0.7], shuffle=True)
privileged_groups = [{'Gender': 1}]
unprivileged_groups = [{'Gender': 0}]
/home/wsuser/work
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-59-8c624cfec261> in <module>
5 # consider in this evaluation
6 privileged_classes=['Male'], # male is considered privileged
----> 7 features_to_drop=[]) # ignore all other attributes
8
9 dataset_orig_train, dataset_orig_test = dataset_orig.split([0.7], shuffle=True)
TypeError: 'DataFrame' object is not callable
It appears that df_data_1 is your dataset dataframe, right? If it is, you would need to update your script to convert it to StandardDataset:
from aif360.datasets import StandardDataset
dataset_orig = StandardDataset(df_data_1,
protected_attribute_names=['Gender'],
privileged_classes=['Male'],
features_to_drop=[],
favorable_classes=[1] # Update this with label values which are considered favorable in your dataset
)
I am not sure how your dataset looks like, but you can adapt the complete reproducible example here to do this process for your dataset.

i got an attribute error couldn't figure this one out so i had to ask here in python3.6 and the pandas data frame work

I am trying to execute the following code:
sapmle2000submission.astype('int32').dtypes
which raises an error:
AttributeError Traceback (most recent call
last) in ()
----> 1 sapmle2000submission.astype('int32').dtypes
AttributeError: 'list' object has no attribute 'astype'
Can someone please help me to figure out why?
Looks like that the obejct sapmle2000submission is a list, not a pandas Series.
You can convert it as Series and specify its dtype:
import pandas as pd
sapmle2000submission_series = pd.Series(sapmle2000submission, dtype='int32')

"TypeError: 'dict' object is not callable" in Python pandas DataFrame

I imported pandas as pd.
>>>import pandas as pd
Assigned Series variables 'city_names' and 'population'
>>> city_names=pd.Series(['San Fransisco','San Jose','Sacromento'])
>>> population=pd.Series([8964,91598,034892])
Created DataFrame
>>> pd.DataFrame=({'City Name': city_names, 'Population':population})
While assigning DataFrame to the variable 'cities',
>>> cities=pd.DataFrame({'City Name ' : city_names, 'Population' : population})
I am getting the following error :
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
cities=pd.DataFrame({'City Name ' : city_names, 'Population' : population})
TypeError: 'dict' object is not callable
Also aware that 'dict' is a dictionary with (key,value) pairs! please let me know why this error occurs. I looked into other questions as well but could'nt find help.
Your code pd.DataFrame=({'City Name': city_names, 'Population':population}) replaced pd.DataFrame function with a dictionary.
#Ankur please modify this edit to your liking.
PiR edit:
pd.DataFrame is a class defined by pandas. A class is "callable" meaning that you can call it with parentheses like so pd.DataFrame(). However, in Python the = is an assignment operator and you did pd.DataFrame = {} which assigned some dictionary to the same spot that the DataFrame class constructor used to be. You should not use that line specified by #Ankur. It will break your stuff every time.

Generate random strings in pandas

I would like to create a string of one million keys with 200 different values:
N = 1000000
uniques_keys = [pd.core.common.rands(3) for i in range(200)]
keys = [random.choice(uniques_keys) for i in range(N)]
However, I get the following error
In [250]:import pandas as pd
In [251]:pd.core.common.rands(3)
Traceback (most recent call last):
File "<ipython-input-251-31d12e0a07e7>", line 1, in <module>
pd.core.common.rands(3)
AttributeError: module 'pandas.core.common' has no attribute 'rands'
I use pandas version 0.18.0.
You can use:
In [14]: pd.util.testing.rands_array?
Signature: pd.util.testing.rands_array(nchars, size, dtype='O')
Docstring: Generate an array of byte strings.
Demo:
In [15]: N = 1000000
In [16]: s_arr = pd.util.testing.rands_array(10, N)
In [17]: s_arr
Out[17]: array(['L6d2GwhHdT', '5oki5T8VYm', 'XKUblAUFyL', ..., 'BE5AdCa62a', 'X3zDFKj6iy', 'iwASB9xZV3'], dtype=object)
In [18]: len(s_arr)
Out[18]: 1000000
UPDATE: from 2020-04-21
In newer Pandas versions you might see the following deprecation warning:
FutureWarning: pandas.util.testing is deprecated. Use the functions in
the public API at pandas.testing instead.
in this case import this function as follows:
from pandas._testing import rands_array
There are several solutions:
First solution:
The function rands appears to be in pandas.util.testing now:
pd.util.testing.rands(3)
Second solution:
Go straight for the underlying numpy implementation (as found in the pandas source code):
import string
RANDS_CHARS = np.array(list(string.ascii_letters + string.digits),
dtype=(np.str_, 1))
nchars = 3
''.join(np.random.choice(RANDS_CHARS, nchars))
Third solution:
Call numpy.random.bytes (check that it fulfils your requirements).
Fourth solution:
See this question for other suggestions.

pandas bug with empty DataFrame?

Here is my code snippet:
>>> import numpy as np
>>> import pandas as pd
>>> pd.__version__
'0.12.0'
>>> np.__version__
'1.6.2'
>>> pd.DataFrame(None,columns=['x'])['x'].values + np.min([0.5])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.float64'
I'm making a sum between an empty array and a scalar. Notice that if I construct the array with np.array or with pd.Series it works fine (the result is an empty array). The same is true if I replace np.min([0.5]) (which is a np.float64) with a float: 0.5.
I find this very strange! Of course I can easily patch my program, but I think this is a bug of pandas, isn't it?
addendum is it correct to initialize pd.DataFrame with None to get an empty DataFrame with given columns?