Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
[Research Question] Shifting time axis for a series

Can anyone tell me how to shift a series of data one unit of time when graphing in the research environment?

For example, I want to compare how Stock A's performance last unit of time (either a day ago or a minute ago) is related to how Stock B will perform now. I want to plot them together, but shift Stock A's data one unit of time so it lines up with "the future" for Stock B.

I'm assuming this is pretty trivial to people well versed in python plotting, but any help would be greatly appreciated. Thanks!

1 response

To bring data from the previous minute forward in time, .shift(1).

df  
Out[59]:  
   A   B  
0  1  10  
1  2  20  
2  3  30  
3  4  40

df.shift(1)  
Out[60]:  
    A   B  
0 NaN NaN  
1   1  10  
2   2  20  
3   3  30  
df['Bshift'] = df['B'].shift(1)

df

Out[63]:  
   A   B  Bshift  
0  1  10     NaN  
1  2  20      10  
2  3  30      20  
3  4  40      30