Home > other >  'numpy.float64' object is not callable with numpy and pandas with custom function
'numpy.float64' object is not callable with numpy and pandas with custom function

Time:05-17

I have code of the form:

import pandas as pd
import numpy as np

def StrdErr(vec):
  return np.std(vec)/np.sqrt(len(vec))

df2 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c'])

for idx_q in range(0, df2.shape[0]):
  StrdErr = StrdErr(np.array(df2.loc[idx_q, :]))

with the following error message:

Traceback (most recent call last):
  File "debug.py", line 11, in <module>
    StrdErr = StrdErr(np.array(df2.loc[idx_q, :]))
TypeError: 'numpy.float64' object is not callable

I saw a similar question with answer but could not solve the problem What am I doing wrong?

CodePudding user response:

This looks like a very complicated way to compute:

df2.std(1, ddof=0).div(np.sqrt(df2.shape[1]))

output:

0    0.471405
1    0.471405
2    0.471405
dtype: float64
even if it is inefficient, to fix your loop use:
out = []
for idx_q in range(0, df2.shape[0]):
  out.append(StrdErr(np.array(df2.loc[idx_q, :])))
print(out)
# [0.47140452079103173, 0.47140452079103173, 0.47140452079103173]

CodePudding user response:

This does the same thing, much more efficiently:

df.sem(ddof=0, axis=1)
  • Related