From 9469d2dce3b62a5372b511385c7787f8e24f8884 Mon Sep 17 00:00:00 2001 From: Brad <1676751+Sinkleberg@users.noreply.github.com> Date: Fri, 6 Nov 2026 07:22:53 -0500 Subject: [PATCH 1/4] Show how to calculate rolling means within groups Assisted-by: Codex --- docs/Project.toml | 2 ++ docs/src/man/split_apply_combine.md | 31 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/docs/Project.toml b/docs/Project.toml index 00c8ce816..2e6254e70 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -10,6 +10,7 @@ InlineStrings = "842dd82b-1e85-43dc-bf29-5d0ee9dffc48" Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28" PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d" Query = "1a8c2f83-1ff3-5112-b086-8aa67b057ba1" +RollingFunctions = "b0e4dd01-7b14-53d8-9b45-175a3e362653" StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" @@ -17,4 +18,5 @@ TidierData = "fe2206b3-d496-4ee9-a338-6a095c4ece80" [compat] Documenter = "1" +RollingFunctions = "0.8.1" StableRNGs = "1" diff --git a/docs/src/man/split_apply_combine.md b/docs/src/man/split_apply_combine.md index ff5d68732..a971ebf5d 100644 --- a/docs/src/man/split_apply_combine.md +++ b/docs/src/man/split_apply_combine.md @@ -519,6 +519,37 @@ julia> gd[1] 1 │ 1 ``` +### Rolling means within groups + +A rolling mean often belongs to the last observation in its window. Sort the +observations by time first, then use `combine` to keep one row per complete +window. Here `rollmean` from [RollingFunctions.jl](https://github.com/JeffreySarnoff/RollingFunctions.jl) +averages two consecutive readings from the same sensor. + +```jldoctest sacrolling +julia> using DataFrames + +julia> using RollingFunctions: rollmean + +julia> readings = DataFrame(sensor=["A", "B", "A", "B", "A"], + day=[3, 2, 1, 1, 2], reading=[14, 6, 10, 4, 12]); + +julia> sort!(readings, [:sensor, :day]); + +julia> windows = combine(groupby(readings, :sensor), + :day => (d -> d[2:end]) => :day, + :reading => (r -> rollmean(r, 2)) => :mean_reading); + +julia> windows == DataFrame(sensor=["A", "A", "B"], day=[2, 3, 2], + mean_reading=[11.0, 13.0, 5.0]) +true +``` + +Each group here has at least two readings. For a group of `N` readings and +a window of width `w`, there are `N - w + 1` complete windows when `N >= w`. +The first result aligns with day 2 in each group. `combine` retains the sensor +key for every window. + ## Using `GroupedDataFrame` as an iterable and indexable object If you only want to split the data set into subsets, use the [`groupby`](@ref) From a01cbc6fe2507619d760feb74feeb8ff742f6882 Mon Sep 17 00:00:00 2001 From: Brad <1676751+Sinkleberg@users.noreply.github.com> Date: Tue, 10 Nov 2026 04:52:50 -0500 Subject: [PATCH 2/4] Use Statistics for the grouped rolling mean example --- docs/Project.toml | 2 -- docs/src/man/split_apply_combine.md | 16 +++++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 2e6254e70..00c8ce816 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -10,7 +10,6 @@ InlineStrings = "842dd82b-1e85-43dc-bf29-5d0ee9dffc48" Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28" PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d" Query = "1a8c2f83-1ff3-5112-b086-8aa67b057ba1" -RollingFunctions = "b0e4dd01-7b14-53d8-9b45-175a3e362653" StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" @@ -18,5 +17,4 @@ TidierData = "fe2206b3-d496-4ee9-a338-6a095c4ece80" [compat] Documenter = "1" -RollingFunctions = "0.8.1" StableRNGs = "1" diff --git a/docs/src/man/split_apply_combine.md b/docs/src/man/split_apply_combine.md index a971ebf5d..68f9ef376 100644 --- a/docs/src/man/split_apply_combine.md +++ b/docs/src/man/split_apply_combine.md @@ -523,13 +523,13 @@ julia> gd[1] A rolling mean often belongs to the last observation in its window. Sort the observations by time first, then use `combine` to keep one row per complete -window. Here `rollmean` from [RollingFunctions.jl](https://github.com/JeffreySarnoff/RollingFunctions.jl) -averages two consecutive readings from the same sensor. +window. The `rolling_mean` helper below uses `mean` from `Statistics` to +average consecutive readings from the same sensor. ```jldoctest sacrolling -julia> using DataFrames +julia> using DataFrames, Statistics -julia> using RollingFunctions: rollmean +julia> rolling_mean(x, w) = [mean(@view x[i:i+w-1]) for i in 1:length(x)-w+1]; julia> readings = DataFrame(sensor=["A", "B", "A", "B", "A"], day=[3, 2, 1, 1, 2], reading=[14, 6, 10, 4, 12]); @@ -538,15 +538,17 @@ julia> sort!(readings, [:sensor, :day]); julia> windows = combine(groupby(readings, :sensor), :day => (d -> d[2:end]) => :day, - :reading => (r -> rollmean(r, 2)) => :mean_reading); + :reading => + (r -> rolling_mean(r, 2)) => :mean_reading); julia> windows == DataFrame(sensor=["A", "A", "B"], day=[2, 3, 2], mean_reading=[11.0, 13.0, 5.0]) true ``` -Each group here has at least two readings. For a group of `N` readings and -a window of width `w`, there are `N - w + 1` complete windows when `N >= w`. +The helper assumes a positive window width. Each group here has at least two +readings. For a group of `N` readings and a window of width `w`, there are +`N - w + 1` complete windows when `N >= w`. The first result aligns with day 2 in each group. `combine` retains the sensor key for every window. From c1e8bac79e08d88f578a2e3b7740924141971c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Tue, 10 Nov 2026 22:34:30 +0100 Subject: [PATCH 3/4] Apply batched suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bogumił Kamiński --- docs/src/man/split_apply_combine.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/src/man/split_apply_combine.md b/docs/src/man/split_apply_combine.md index 68f9ef376..4d6bfbcb5 100644 --- a/docs/src/man/split_apply_combine.md +++ b/docs/src/man/split_apply_combine.md @@ -536,10 +536,10 @@ julia> readings = DataFrame(sensor=["A", "B", "A", "B", "A"], julia> sort!(readings, [:sensor, :day]); +julia> w = 2; # window width julia> windows = combine(groupby(readings, :sensor), - :day => (d -> d[2:end]) => :day, - :reading => - (r -> rolling_mean(r, 2)) => :mean_reading); + :day => (d -> d[w:end]) => :day, + :reading => (r -> rolling_mean(r, w)) => :mean_reading); julia> windows == DataFrame(sensor=["A", "A", "B"], day=[2, 3, 2], mean_reading=[11.0, 13.0, 5.0]) @@ -548,9 +548,9 @@ true The helper assumes a positive window width. Each group here has at least two readings. For a group of `N` readings and a window of width `w`, there are -`N - w + 1` complete windows when `N >= w`. +`N - w + 1` complete windows when `N >= w`. If `N < w` the group is dropped. The first result aligns with day 2 in each group. `combine` retains the sensor -key for every window. +key for every window as in this case each group has at least `w = 2` observations. ## Using `GroupedDataFrame` as an iterable and indexable object From 9d81463fe749f2face218edcc5704a163ab81916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Tue, 10 Nov 2026 22:35:36 +0100 Subject: [PATCH 4/4] Apply suggestion from @bkamins --- docs/src/man/split_apply_combine.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/man/split_apply_combine.md b/docs/src/man/split_apply_combine.md index 4d6bfbcb5..1bd69e369 100644 --- a/docs/src/man/split_apply_combine.md +++ b/docs/src/man/split_apply_combine.md @@ -537,6 +537,7 @@ julia> readings = DataFrame(sensor=["A", "B", "A", "B", "A"], julia> sort!(readings, [:sensor, :day]); julia> w = 2; # window width + julia> windows = combine(groupby(readings, :sensor), :day => (d -> d[w:end]) => :day, :reading => (r -> rolling_mean(r, w)) => :mean_reading);