Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help on Q IDE Type Error - Only length-1 arrays can be converted to Python scalars

I'm new to Python, and don't understand this IDE error if the TA-Lib function is supposed to return a scalar, presumably a float. 'Close' is a NumPy array of close pricing, and represents resampled pricing data that is expected to be complete by this exchange time stamp. Does the Q IDE interpret MACDValue to be an array?

        MACDValue = 0

        MACDValue = ta.EMA(Close, MACDShtLen) - ta.EMA(Close, MACDLngLen)  
        if math.isnan(MACDValue) :  
            MACDValue = 0

        TypeError: only length-1 arrays can be converted to Python scalars  
        USER ALGORITHM:875, in handle_data  
        if math.isnan(MACDValue) :  
8 responses

Best advice I can give is to print out the results of EMA, which I suspect you'll find is vector-shaped, and then change MACDValue to be xxx.[-1] or something. http://victorarias.com.br/images/duck_typing.jpg https://pbs.twimg.com/media/B3Fvg-sCYAAkLSV.jpg:large

If I remove the test code for 'if math.isnan(MACDValue)' then I get a related error in a later statement for the MACDValue scalar acknowledging that it IS a scalar float...

                ValueError: cannot convert float NaN to integer  
                USER ALGORITHM:884, in handle_data  
                MACDValFlat = int(context.A_MACDValue[1]*FlatMult) == int(MACDValue*FlatMult)  

Here is a statement and error isolating the MACDValue...

                     ValueError: cannot convert float NaN to integer  
                     USER ALGORITHM:885, in handle_data  
                     TestVal = int(MACDValue*FlatMult)  

Hi Mark,

It looks as though you have some NaNs in your data. I would do something like

dataframe = dataframe.dropna()  

Let me know if that works and we can work from there.

Seong

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.

Seong, does dropna() collapse the array by dropping indexes containing NaNs, or can I preserve the index sequence by forwardfilling the NaN using a different statement?

There is the option of fillna()Would suggest taking a look at http://pandas.pydata.org/pandas-docs/stable/missing_data.html, goes over really clearly the best ways to deal with missing data that fit your goals

Thanks,
Seong

Thanks Seong!

Getting back to the subject error, what is going on with the type error 'Only length-1 arrays can be converted to Python scalars' when the IDE later 'acknowledges' that the value is a scalar float?

I think the two are very related, it may just be that the error is happening because you're calling two different methods on a NaN value hence the first error: 'Only length-1 arrays can be converted to Python scalars' and the second error: "ValueError: cannot convert float NaN to integer" Although I'm not 100% sure.

If you give me more details, I can get you a better explanation if the above doesn't make sense.