Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pipeline: syntax question

Hi,

I attached a piece of code below. I am wondering how I can output both factor_ranks and factors as part of my pipeline. Right now, I can output each individual but I would like to combine them so that there are two columns for each rank, one for the factor value and one for it's rank.

factors = make_factors()

def make_pipeline(factors):  
    factor_ranks = {name_rank: f().rank(mask=base_universe) for name_rank, f in factors.iteritems()}  
    factors = {name: f(mask=base_universe) for name, f in factors.iteritems()}  
    factor_ranks['Sector'] = Sector()  
    return Pipeline(columns=factor_ranks,  
                    screen=base_universe )  
2 responses

any help?

The terms 'factor_ranks' and 'factors' are python dictionaries with the factor names as keys, and the associated factor ranks as values. To combine those two dictionaries one can use the 'update' method. Take a look at the docs here https://docs.python.org/2/library/stdtypes.html#dict.update.

So something like this should work (though I haven't tried it).

factors = make_factors()

def make_pipeline(factors):  
    factor_ranks = {name_rank: f().rank(mask=base_universe) for name_rank, f in factors.iteritems()}  
    factors = {name: f(mask=base_universe) for name, f in factors.iteritems()}  
    factor_ranks['Sector'] = Sector()  


    # Append the factor_rank dict to the factor dict  
    factors.update(factor_ranks)  


    return Pipeline(columns=factors,  
                    screen=base_universe )