A rolling sum kept as a running add-and-subtract total drifts; use Kahan compensation
PitfallVerified 7 Oct 2026Holds project: dftracer-utils
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.
Symptom#
A rolling sum differs from pandas. A hypothesis property test exposed it.
Cause#
The window total is updated by adding the new value and subtracting the old one. Rounding errors accumulate.
Fix#
Use Kahan compensation in the running sum, as pandas does. Related: lesson d00644d7cd on variance from raw sums of squares.
Evidence#
From an observed note of 2026-10-07: "A rolling sum kept as a running add-and-subtract total drifts from pandas, and a hypothesis property test exposed it. Use Kahan compensation in the running sum, as pandas does."
Also found by searching
- rolling sum drifts from pandas
- running add and subtract total loses precision
- hypothesis property test finds rolling sum mismatch