Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
ValueError: setting an array element with a sequence.
class PD(CustomFactor):  
    inputs = [morningstar.asset_classification.financial_health_grade]  
    window_length = 1  
    def compute(self, today, assets, out, financial_health_grade):  
        print(len(financial_health_grade))  
        out[:] = financial_health_grade[-1]  

When I add this to pipeline, it returns ValueError: setting an array element with a sequence.

How can I solve this?

2 responses

Hello,

If you just want the latest value of the fundamental (window_length=1) then use:

Fundamentals.financial_health_grade.latest  

A couple of things. Probably use the 'Fundamentals' namespace and not 'morningstar' to get fundamental data (ie Fundamentals.financial_health_grade). Check out this post https://www.quantopian.com/posts/faster-fundamental-data .

The basic issue is that 'financial_health_grade' returns a string (eg A or B). Python thinks of strings as ordered sequences of character data. Factors return numerical data. Hence, the error "ValueError: setting an array element with a sequence". One could do a bit of logic in the custom factor to translate the string grade to a numerical grade. Alternately, one could create a custom classifier instead but that isn't well documented and a bit more involved than a custom factor (one must specify the datatype). For more info on classifiers maybe see this post https://www.quantopian.com/posts/pipeline-classifiers-are-here .

I'd stick with @Mathieu Meynier suggestion and use the 'latest' method.