在Pandas DataFrame,numpy数组和float之间减去和相乘

时间:2016-03-09 00:14:48

标签: python-2.7 numpy pandas

我目前正在尝试执行以下代码行:

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

我很困惑,因为我认为pandas Dataframes和numpy数组可以“互相交谈”,并且我会得到一个DataFrame或Series,并逐行进行计算。

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

有人可以强调我出错的地方和我误解的地方。

1 个答案:

答案 0 :(得分:1)

您的一个阵列已转置。

尝试X_array.shapey.shape

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

你可以尝试:

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

将公式应用于DataFrame y的每个系列。

如果这不起作用,请尝试:

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