这里有点困惑。我有一些代码可以直接运行:
results = INPUT
dates = set(results[0])
sectors = set(results[2])
columns = ["Port Wt", "Bench Wt", "Port Retn", "Bench Retn", "Attrib", "Select", "Inter", "Total"]
results.fillna(0,inplace=True)
sectors = sorted(sectors)
test = np.ones([len(sectors) + 1, len(columns)])
for date in dates:
day = results[results[0] == date]
for n, sector in enumerate(sectors):
s = day[day[2] == unicode(sector)]
pw = s.iloc[:,4].sum()
bw = s.iloc[:,6].sum()
pr = s.apply(lambda x: x.ix[5]/100 * x.ix[4], axis=1).sum().sum()
br = s.apply(lambda x: x.ix[5]/100 * x.ix[6], axis=1).sum().sum()
test[n,:] *= [(1. + pw)**(1./len(dates)), (1.+bw) ** (1./len(dates)), 1. + pr , 1. + br , 1. ,
1. , 1. , 1.]
test -= 1.
但是当我在web2py控制器中运行它时
def test():
portfolio = "usequity"
benchmark = "SP500"
startdate = "2013-01-11 00:00:00"
enddate = "2013-01-30 00:00:00"
level1 = 'sector'
level2 = 'industry'
i1 = datadrawn.results(portfolio,benchmark,startdate,enddate,level1,level2,cr)
i2 = datadrawn.sectors(i1['portfolio'], i1['benchmark'])
i3 = datadrawn.brinson(i2)
return dict(i3=i3)
我收到错误
<type 'exceptions.TypeError'>(unsupported operand type(s) for *=: 'numpy.ndarray' and 'list')
局部变量对我来说没问题..
locals request session response
locals
INPUT : 0 4 ... ... ... ... [6422 rows x 7 columns]
br : 0.0002428845794774927
bw : 0.12515205797541681
columns : ['Port Wt', 'Bench Wt', 'Port Retn', 'Bench Retn', 'Attrib', 'Select', 'Inter', 'Total']
date : u'2013-01-11 00:00:00'
dates : set([u'2013-01-11 00:00:00', u'2013-01-14 00:00:00', u'2013-01-15 00:00:00', u'2013-01-16 00:00:00', u'2013-01-17 00:00:00', u'2013-01-18 00:00:00', ...])
day : 0 4 ..... ... ... [494 rows x 7 columns]
n : 0
pr : -9.970633274708913e-05
pw : 0.075036537596357311
results : 0 4 ... ... ... ... [6422 rows x 7 columns]
s : 0 4 ...... ... ... [82 rows x 7 columns]
sector : u'Consumer Discretionary'
sectors : [u'Consumer Discretionary', u'Consumer Staples', u'Energy', u'Financials', u'Health Care', u'Industrials', u'Information Technology', u'Materials', u'Telecommunications Services', u'Utilities']
test : array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],...0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]], dtype=float64)
一些研究表明,这可能是阵列错误排列,但据我所知,我已将所有内容转换回浮动。
非常感谢任何帮助。