from pandas import Series, date_range
import numpy as np
%matplotlib inline
ts = pd.Series(np.random.randn(1000), index=date_range('1/1/2000', periods=1000))
ts = ts.cumsum() + 100
ts.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x10ad89150>
# Define a function to calculate prct_change over x-days
rollingChange = lambda x: 100*(x[-1]-x[0])/x[0]
# Use a rolling_apply to get 7 and 30 day rolling prct_change
week = pd.rolling_apply(ts, 7, rollingChange)
month = pd.rolling_apply(ts, 30, rollingChange)
week.head
<bound method Series.head of 2000-01-01 NaN 2000-01-02 NaN 2000-01-03 NaN 2000-01-04 NaN 2000-01-05 NaN 2000-01-06 NaN 2000-01-07 -2.642893 2000-01-08 -2.883829 2000-01-09 -1.312829 2000-01-10 -1.302637 2000-01-11 -2.567666 2000-01-12 -2.751872 2000-01-13 0.338794 2000-01-14 1.949121 2000-01-15 -0.272679 ... 2002-09-12 -1.854274 2002-09-13 -2.455134 2002-09-14 -1.383926 2002-09-15 -1.768071 2002-09-16 0.155018 2002-09-17 1.108601 2002-09-18 0.113923 2002-09-19 0.679943 2002-09-20 -2.072446 2002-09-21 -1.333065 2002-09-22 -3.937603 2002-09-23 -5.529000 2002-09-24 -4.232423 2002-09-25 -3.625264 2002-09-26 -2.129178 Freq: D, Length: 1000>
month.head
<bound method Series.head of 2000-01-01 NaN 2000-01-02 NaN 2000-01-03 NaN 2000-01-04 NaN 2000-01-05 NaN 2000-01-06 NaN 2000-01-07 NaN 2000-01-08 NaN 2000-01-09 NaN 2000-01-10 NaN 2000-01-11 NaN 2000-01-12 NaN 2000-01-13 NaN 2000-01-14 NaN 2000-01-15 NaN ... 2002-09-12 -0.666639 2002-09-13 -3.113743 2002-09-14 -3.367993 2002-09-15 -4.912028 2002-09-16 -1.900453 2002-09-17 -1.565117 2002-09-18 -2.993793 2002-09-19 -3.523837 2002-09-20 -5.883148 2002-09-21 -3.802819 2002-09-22 -4.384001 2002-09-23 -4.917181 2002-09-24 -3.059335 2002-09-25 -2.635329 2002-09-26 -4.285938 Freq: D, Length: 1000>