Reputation: 1256
I am trying to apply my dispersion function :
def disp_calc(returns, p, wi): #apply(disp_calc, rows = ...)
wi = wi/np.sum(wi)
ri_p = returns_stock.subtract(p,axis=0)
rp = np.nansum(wi*((ri_p)**2))
return np.sqrt(rp)
To my returns_sector df below:
for i in sectors:
returns_sector = returns[sectordict[i]]
pr = returns[sectordict[i]].apply(np.mean,axis=1)
w = newmerge.Weight[newmerge.SectorSymbol == i]
disp_df[date] = returns_sector.apply(disp_calc(returns_sector,pr,w),axis=1)
I keep getting the error
:
TypeError: ("'numpy.float64' object is not callable", u'occurred at index 2017-01-03 00:00:00')
I've never had this problem with apply before and not really sure what I'm doing wrong. My expected output is to apply the disperson function to all the values in my returns_sector df
.
Upvotes: 1
Views: 365
Reputation: 1961
You're not correctly passing arguments to your disp_calc
function. Try this:
disp_df[date] = returns_sector.apply(disp_calc, args=(pr, w), axis=1)
The calling dataframe is automatically passed as the first argument to the function.
The error your're seeing is that your current first argument to apply()
is the float returned by disp_calc()
, which is not a function, and thus not a callable. Pandas expects the first argument in the apply()
function to be callable.
Upvotes: 1