Python - 尝试计算时间 t 之前事件的平均时间

Python - Attempting to calculate the average time of events prior to time t

这是一个示例数据集:

ID  Date
1   2/3/18
1   2/7/18
1   2/14/18
1   2/16/18

最终功能如下所示:

ID  Date     Running_Mean
1   2/3/18   0
1   2/7/18   4
1   2/14/18  5.5
1   2/16/18  4.33

这是一个滚动 window,它从序列的开头开始,并随着数据集继续扩展。

如有任何帮助,我们将不胜感激。

通过使用 expandingwindows = len(df)

时与 rolling 相同
df.Date=pd.to_datetime(df.Date)

df.Date.diff().dt.days.expanding(1).mean()
Out[654]:
0         NaN
1    4.000000
2    5.500000
3    4.333333
Name: Date, dtype: float64