Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pandas Multiplication not working

Hi everyone

Not sure what it is happening with the pandas dataframe multiplication. I have try 2 different ways and not working. I am trying to multiply the data frame where the returns data frame has the series multiply by the weights. Can you guys check what I am doing wrong?

import pandas as pd  
import numpy as np  
from pandas import DataFrame as df

def initialize(context):  
    # Place orders with the order(SID, amount) method.  
    context.secs = [sid(24),sid(8554),sid(46632)]  

    pass

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    # Implement your algorithm logic here.  
    hist = history(80, '1d','price',ffill=True)  
    weights = [10,0,0]  
    pd_all= pd.Series(weights,index=context.secs)  
    #First Option not working multiplication  for the returns  
    returns= pd.DataFrame()  
    for eq in context.secs:  
        quant= (float(pd_all[eq])*float(.99))/ float(10)  
        returns[eq] = hist[eq] * quant

    # Second option not doing multiplication  
    prices = np.log(history(50, '1d', 'price'))  
    assets = prices[context.secs]  
    returns = assets * pd_all  
    print assets  
```e
11 responses

I don't understand the question. Can you explain more clearly what you are trying to do?

Hi Epic

In the result dataframe needs to be a value that has been multiply for the weight position but it is not happening look the debugger
###### APPL, SPY, GOOG ****
weights = [10,0,0]

You are printing "assets", and not the returns! For me it did return the multiplied values. Are you sure it's not working?

(i.e. instead of "print assets" do "print returns"? Because the returns vector is the one that's supposed to be multiplied?)

Yep im sure that returns and assets are not getting properly multiply if I use assets it does not multiply SPY by Zero and if I use returns APPL doesnt get mutiply by .99

I have printed both options and none of them work. The array returns or assets should look like the following:

Series (APPL ): 4.xxxx
.....
4.xxxx
3.xxxx
Series (SPY): 0
...
0
0
Series (GOOG): 0
...
0
0

Yeah for me it does, I'll show you a screenshot: image

Wooww I think that the debugger it is broken let me show you, When I print the values they are fine in the LOG but in the debugger is not.
Am I missing something?

If you are missing something, then so am I. Looks weird to me.

By the way, your last ticker (sid(46631)) is bad, the call to history only gives NaN, change it to something else :)

print hist[sid(46632)]  

2014-03-28PRINT2013-12-03 00:00:00+00:00 NaN
2013-12-04 00:00:00+00:00 NaN
2013-12-05 00:00:00+00:00 NaN
2013-12-06 00:00:00+00:00 NaN
2013-12-09 00:00:00+00:00 NaN
2013-12-10 00:00:00+00:00 NaN

@Erick this is indeed a bug in the debugger: https://www.quantopian.com/posts/question-on-the-history-function-in-debugger-mode

We're aware of it and will be working to get it fixed! The values printed in your logs are correct.

Thanks,
Alisa

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

OK Thanks Alisa

EG

With the recent debugger changes you might want to check this again and see if ok now.