<jupyter_start><jupyter_text>Window function * rolling window --> how did i do last three days --> check everyday * expanding window --> all data equallu relevant --> old or new<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt

%pylab inline
pylab.rcParams['figure.figsize'] = (19,6)
import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>special thing about timeseries * data points relate to one another * not independent * campare and relate them * one way to do that is to look at how they change<jupyter_code>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()
plt.plot(ts, color='blue')
plt.plot(ts_lagged, color='red')
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)
plt.plot(ts2, color='blue')
plt.plot(ts2_lagged, color='red')
ts3_lagged = ts2.shift(-5)
plt.plot(ts2, color='blue')
plt.plot(ts2_lagged, color='red')
plt.plot(ts3_lagged, color='black')<jupyter_output><empty_output><jupyter_text>moving aggregate measures of time series* window functions are like aggregate functions* it can be used in conjuction with .resample()<jupyter_code>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()
df.plot()
df.index
# window averaging over 20 days coz freq days.
# window is simply row count
r = df.rolling(window=20)
r
df['A'].plot(color='red')
r.mean()['A'].plot(color='blue')
r.count().head()
r.A.count().head()
r.quantile(.5).plot()
r.agg( [ 'sum', 'var' ])[15:25]<jupyter_output><empty_output><jupyter_text>rolling custom function * .apply()<jupyter_code>df.rolling(window=10, center=False).apply(lambda x: x[1] / x[2])[1:30]<jupyter_output><empty_output><jupyter_text>generate rolling window function of monthly data from daily data<jupyter_code>ts_long = pd.Series(np.random.rand(200), 
                    pd.date_range('7/1/16', 
                                  freq='D', 
                                  periods=200))
ts_long.head()
# rolling window of 3 month at a time
ts_long.resample('M').mean().rolling(window=3).mean()
ts_long.resample('M').mean().rolling(window=3).mean().plot()<jupyter_output><empty_output><jupyter_text>expanding window<jupyter_code>df.expanding(min_periods=1).mean()[1:5]
df.expanding(min_periods=1).mean().plot()<jupyter_output><empty_output><jupyter_text>exponentially weight moving average<jupyter_code>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()
ts_ewma.ewm(span=60, freq='D', min_periods=0, adjust=True).mean().plot()
ts_ewma.rolling(window=60).mean().plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1497079618813403, 'fraction_numerical': 0.02951122041192745, 'mean_word_length': 3.072590738423029, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '119409', 'license': 'MIT', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1083, 'n_words': 219, 'path': 'jupyter/pandas_moving_window_functions.ipynb', 'repo_name': 'ayushmaskey/ayushmaskey.github.io', 'size': '3253'}