Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can i get this DataFrame?

I want to add a column to the result of the pipeline , that represents the returns that a certain stock is gonna have next period. Something like shifting the returns factor column X days backwards, but keeping it aligned with everything else, so I can pass it to a ML classifier with the factors as the features and the returns as the labels. I dont know if i´m making myself clear. Of course this is within the research enviroment.

2 responses

If I understand correctly you want to add a column to the pipeline output that is the 'shifted' returns from sometime in the future. Something like this should work.

# 'results' is the panel returned by a pipeline  
# this assumes there is a column named 'returns' included as a factor (column)


results.assign(shifted_returns=results.groupby(level=1).returns.shift(-1))


# assign will create a copy of 'results'  
# to modify 'results' in place then this works  
results['shifted_returns'] = results.groupby(level=1).returns.shift(-1)

One can change the shift parameter from -1 to whatever is desired.

See attached notebook. Good luck with the ML approach.

This is exactly what I was looking for, thanks!