0

I am currently trying to carry out the following line of code:

z = y-(X_array*HedgeRatio)

with the variables having the following attributes:

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

But when it executes I get the following long error message:

---------------------------------------------------------------------------
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

I am confused as I thought pandas Dataframes and numpy arrays could "talk" to eachother and that I would get a DataFrame or Series with the calculations outlined carried out row by row.

The length of the numpy array and the DataFrame included in the line of code both have the same length.

Could someone please highlight where I am going wrong and what I have misunderstood.

1 Answer 1

1

One of your arrays is transposed.

Try X_array.shape and y.shape.

y is also a dataframe, and it is not clear how many columns it contains.

You could try:

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

Which would apply the equation to each series of the DataFrame y.

If that doesn't work, try:

 z = y.apply(lambda s: s - np.matrix(X_array).T * HedgeRatio)
Sign up to request clarification or add additional context in comments.

1 Comment

Ah many thanks for hint as to what was going wrong - I guess I should have figured it out when the error message said "1554 items were passed" instead of 1. I used the .reshape function to reshape my array earlier in my script "X_array = X_array.reshape(len(X_array),1)" and it now works. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.