Skip to content

Commit 1058c32

Browse files
authored
DOC: add time-distance weighted rolling aggregation example (#63951)
1 parent 8d7afc7 commit 1058c32

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

doc/source/user_guide/cookbook.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,48 @@ Rolling Apply to multiple columns where function returns a Scalar (Volume Weight
861861
Timeseries
862862
----------
863863

864+
Time-distance weighted rolling aggregations
865+
*******************************************
866+
867+
A decay-weighted aggregation can be applied inside a time-based rolling window.
868+
The weight depends on how far each observation is from the most recent timestamp in the window.
869+
This is useful when more recent values should contribute more to rolling statistics than older
870+
ones, particularly where observations are not evenly spaced in time.
871+
872+
This approach relies on ``rolling.apply`` and recomputes weights for each window, so it
873+
may be slow for large datasets.
874+
875+
.. code-block:: python
876+
877+
df = pd.DataFrame(
878+
{
879+
"timestamp": [
880+
"2024-01-01 01:00",
881+
"2024-01-01 02:00",
882+
"2024-01-01 05:00",
883+
"2024-01-01 05:00",
884+
"2024-01-01 07:00",
885+
"2024-01-01 08:00",
886+
],
887+
"value": [12, 13, 20, 26, 24, 27],
888+
}
889+
)
890+
891+
df["timestamp"] = pd.to_datetime(df["timestamp"])
892+
df = df.set_index("timestamp")
893+
894+
def decay_weighted_mean(values, alpha=0.1):
895+
timestamps = values.index
896+
age_hours = (timestamps.max() - timestamps).total_seconds() / 3600
897+
weights = np.exp(-alpha * age_hours)
898+
return (weights * values).sum() / weights.sum()
899+
900+
result = (
901+
df["value"]
902+
.rolling("7h")
903+
.apply(decay_weighted_mean)
904+
)
905+
864906
`Between times
865907
<https://stackoverflow.com/questions/14539992/pandas-drop-rows-outside-of-time-range>`__
866908

0 commit comments

Comments
 (0)