Jed Rembold
March 4, 2026
You should always ensure your data is sorted before computing a rolling statistic on it
In Python, with pandas:
df = df.sort_values(colname)In R, with Tidyverse:
df <- arrange(df, colname)Computing the rolling statistic is then straightforward:
df['rolling'] = df.col.rolling(wsize).statistic()
where col is whatever column you are
computing the average over, and wsize is the
size of the window you want
wsize is just a number of data
pointsstatistic is usually either
mean() or
median()
Default window placement is right-aligned, so window comes before data
Easiest to use rollmean (or
rollmedian) from the
zoo library
library(zoo)
df <- df %>%
mutate(
rolling = rollmean(colname, k=wsize)
)wsize needs to be odd for
rollmedian
Default window placement is centered
NaNs or NAsmin_periods=1 to have
the window “grow” out on edgesfill='extend' to have it
pad out NA with the closest value
bls_search function for yousource() the
appropriate file
bls.r will for sure work, but it will be
much slower (about 7x in my testing)
bls_fast.r compiles some C++ code to run
in R that makes it the same speed as the Python version
xcode-select --install in terminalbls_search includes both
period and power columns, so creating a periodogram is easy