2016-03-09 77 views
0

我目前正在开展的下面一行代码:减法熊猫据帧之间相乘,numpy的阵列和浮

z = y-(X_array*HedgeRatio) 

与具有以下属性变量:

Hedge Ratio = 0.489552919785 
Hedge Ratio type = <type 'numpy.float64'> 

y type = <class 'pandas.core.frame.DataFrame'> 
X_array type = <type 'numpy.ndarray'> 

y length = 1554 
X_array length = 1554 

但当它执行时,我得到以下长的错误信息:

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-161-6dd37e0445e9> in <module>() 
     1 #for i in get_symb_list(symbs): 
----> 2 MRAnalysis(symbs,'2010/01/01') 

<ipython-input-160-be6f6d5b64d0> in MRAnalysis(symbList, start_date) 
    63 
    64   #create new spread series by subtracting (X variable price multiplied by hedge ratio) from y price series 
---> 65   z = y-(X_array*HedgeRatio) 
    66 
    67   #plot spread series showing mean of series 

C:\Python27\lib\site-packages\pandas\core\ops.pyc in f(self, other, axis, level, fill_value) 
    838      # casted = self._constructor_sliced(other, 
    839      #         index=self.columns) 
--> 840      casted = pd.Series(other, index=self.columns) 
    841     return self._combine_series(casted, na_op, fill_value, 
    842            axis, level) 

C:\Python27\lib\site-packages\pandas\core\series.pyc in __init__(self, data, index, dtype, name, copy, fastpath) 
    216          raise_cast_failure=True) 
    217 
--> 218     data = SingleBlockManager(data, index, fastpath=True) 
    219 
    220   generic.NDFrame.__init__(self, data, fastpath=True) 

C:\Python27\lib\site-packages\pandas\core\internals.pyc in __init__(self, block, axis, do_integrity_check, fastpath) 
    3381    block = make_block(block, 
    3382        placement=slice(0, len(axis)), 
-> 3383        ndim=1, fastpath=True) 
    3384 
    3385   self.blocks = [block] 

C:\Python27\lib\site-packages\pandas\core\internals.pyc in make_block(values, placement, klass, ndim, dtype, fastpath) 
    2099 
    2100  return klass(values, ndim=ndim, fastpath=fastpath, 
-> 2101     placement=placement) 
    2102 
    2103 

C:\Python27\lib\site-packages\pandas\core\internals.pyc in __init__(self, values, placement, ndim, fastpath) 
    75    raise ValueError('Wrong number of items passed %d,' 
    76        ' placement implies %d' % (
---> 77         len(self.values), len(self.mgr_locs))) 
    78 
    79  @property 

ValueError: Wrong number of items passed 1554, placement implies 1 

我是co因为我认为熊猫数据框架和numpy数组可以“交谈”彼此,我会得到一个DataFrame或系列与计算概述进行逐行。

numpy数组的长度和代码行中包含的DataFrame长度都相同。

是否有人可以指出我出错的地方以及我误解了什么。

回答

1

其中一个阵列发生了转置。

尝试X_array.shapey.shape

y也是一个数据帧,它不清楚它包含多少个列。

你可以尝试:

z = y.apply(lambda s: s - X_array * HedgeRatio) 

这将公式应用到各个系列数据框y的。

如果还是不行,请尝试:

z = y.apply(lambda s: s - np.matrix(X_array).T * HedgeRatio) 
+0

对啊暗示千恩万谢,以什么打算错了 - 我想我应该想到这一点时,该错误消息说,“1554个项目是通过”而不是1.我使用.reshape函数在我的脚本“X_array = X_array.reshape(len(X_array),1)”中对之前的数组进行了整形,现在它可以工作。谢谢你的帮助! – s666