Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can a Factor be defined to produce a DataFrame instead of a Series?

In the documentation, there is a section that reads

" Suppose that f is a Factor which would produce the following output:

        AAPL   MSFT    MCD     BK  
2017-03-13    1.0    2.0    3.0    4.0  
2017-03-14    1.5    2.5    3.5    1.0  
2017-03-15    2.0    3.0    4.0    1.5  
2017-03-16    2.5    3.5    1.0    2.0

But I can't seem to make factors output anything other than series that are indexed by asset. At the moment my goal is to have a factor output a dataframe like the one below where the capital letters are assets and I have alternating rows of open prices, close prices...

     A      B      C      D    .  .  
1    Ao1    Bo1    Co1    Do1  .  .  
2    Ac1    Bc1    Cc1    Dc1  .  .  
3    Ao2    Bo2    Co2    Do2  .  .  
4    Ao1    Bo1    Co1    Do1  .  .  
.    .      .      .      .    .
.    .      .      .      .       .

Any help would be much appreciated!

1 response

late reply, but for anyone else who searches:

class Value(CustomFactor):
# value factor based on OCF/EV
inputs = [mstar.cash_flow_statement.operating_cash_flow,
mstar.valuation.enterprise_value]
window_length = 1

def compute(self, today, assets, out, ocf, ev):  
    factor_df = pd.DataFrame(index=assets)  
    factor_df["ocf"] = ocf[-1]  
    factor_df["ev"] = ev[-1]  
    out[:] = (factor_df['ocf'] / factor_df['ev'])