<jupyter_start><jupyter_text>Description Understand rolling window functionshttps://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html  Imports<jupyter_code>%load_ext blackcellmagic
import pandas as pd<jupyter_output><empty_output><jupyter_text>Fixed number of rows per rolling window<jupyter_code>df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]})
df
df.rolling??
df.assign(win=lambda x: x.rolling(window=2, win_type='triang').sum())
df.assign(win=lambda x: x.rolling(window=2, win_type='gaussian').sum(std=3))
df.assign(win=lambda x: x.rolling(window=2, win_type=None).sum())
df.assign(win=lambda x: x.rolling(window=2, min_periods=1, win_type=None).sum())<jupyter_output><empty_output><jupyter_text>Time windows A ragged (meaning not-a-regular frequency), time-indexed DataFrame<jupyter_code>df = pd.DataFrame(
    {"B": [1, 1, 2, np.nan, 4]},
    index=[
        pd.Timestamp("20130101 09:00:00"),
        pd.Timestamp("20130101 09:00:01"),
        pd.Timestamp("20130101 09:00:05"),
        pd.Timestamp("20130101 09:00:09"),
        pd.Timestamp("20130101 09:00:10"),
    ],
)
df
df.rolling('2s').sum()
df.assign(win=lambda x: x.rolling('2s').sum())
df.dtypes
df.index.dtype<jupyter_output><empty_output><jupyter_text>Index of first row of window If I have a df and I have rolling time windows, I want to know the index of the first row in the window for each row.I will use this outside of pandas to do some aggregates.<jupyter_code>tmp = (df
 .assign(idx=lambda x: range(len(x)))
 .assign(win=lambda x: x.rolling('2s')['B'].sum(),)
 .assign(win_min_idx=lambda x: x.rolling('2s')['idx'].min(),
         win_max_idx=lambda x: x.rolling('2s')['idx'].max())
)
tmp<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16676418958455236, 'fraction_numerical': 0.052077238150965474, 'mean_word_length': 5.653696498054475, 'pattern_counts': {'":': 1, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '608835', 'license': 'FTL', 'n_tokens_mistral': 686, 'n_tokens_neox': 625, 'n_words': 131, 'path': 'notebooks/06-rolling-window-functions-pandas.ipynb', 'repo_name': 'bartev/python-toolbag', 'size': '1709'}