In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
%pylab inline
pylab.rcParams['figure.figsize'] = (19,6)
In [2]:
import numpy as np
import pandas as pd
In [9]:
ts = pd.Series(np.random.randn(20),
pd.date_range('7/1/16',
freq='D',
periods=20))
# shift my one period --> here one day
ts_lagged = ts.shift()
In [10]:
plt.plot(ts, color='blue')
plt.plot(ts_lagged, color='red')
Out[10]:
In [11]:
ts2 = pd.Series(np.random.randn(20),
pd.date_range('7/1/16',
freq='H',
periods=20))
# shift my one period --> here hourly --> but shift 5 hour
ts2_lagged = ts2.shift(5)
In [12]:
plt.plot(ts2, color='blue')
plt.plot(ts2_lagged, color='red')
Out[12]:
In [13]:
ts3_lagged = ts2.shift(-5)
plt.plot(ts2, color='blue')
plt.plot(ts2_lagged, color='red')
plt.plot(ts3_lagged, color='black')
Out[13]:
In [25]:
df = pd.DataFrame( np.random.randn(600, 3),
index = pd.date_range('5/1/16',
freq='D',
periods=600),
columns = ['A', 'B', 'C'])
df.head()
Out[25]:
In [26]:
df.plot()
Out[26]:
In [27]:
df.index
Out[27]:
In [28]:
# window averaging over 20 days coz freq days.
# window is simply row count
r = df.rolling(window=20)
r
Out[28]:
In [29]:
df['A'].plot(color='red')
r.mean()['A'].plot(color='blue')
Out[29]:
In [34]:
r.count().head()
Out[34]:
In [35]:
r.A.count().head()
Out[35]:
In [42]:
r.quantile(.5).plot()
Out[42]:
In [49]:
r.agg( [ 'sum', 'var' ])[15:25]
Out[49]:
In [47]:
df.rolling(window=10, center=False).apply(lambda x: x[1] / x[2])[1:30]
Out[47]:
In [50]:
ts_long = pd.Series(np.random.rand(200),
pd.date_range('7/1/16',
freq='D',
periods=200))
ts_long.head()
Out[50]:
In [53]:
# rolling window of 3 month at a time
ts_long.resample('M').mean().rolling(window=3).mean()
Out[53]:
In [54]:
ts_long.resample('M').mean().rolling(window=3).mean().plot()
Out[54]:
In [55]:
df.expanding(min_periods=1).mean()[1:5]
Out[55]:
In [56]:
df.expanding(min_periods=1).mean().plot()
Out[56]:
In [65]:
ts_ewma = pd.Series(np.random.rand(1000),
pd.date_range('7/1/16',
freq='D',
periods=1000))
ts_ewma.ewm(span=60, freq='D', min_periods=0, adjust=True).mean().plot()
Out[65]:
In [64]:
ts_ewma.ewm(span=60, freq='D', min_periods=0, adjust=True).mean().plot()
ts_ewma.rolling(window=60).mean().plot()
Out[64]:
In [ ]: