Search code examples
pythonpandasrolling-average

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


Here is a sample dataset:

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

Here is what the final feature will look like:

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

This is a rolling window that starts at the beginning of a sequence and continues to expand with the dataset.

Any help would be much appreciated.


Solution

  • By using expanding same thing with rolling when windows = len(df)

    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