1

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.

cs95
  • 379,657
  • 97
  • 704
  • 746
madsthaks
  • 2,091
  • 6
  • 25
  • 46

1 Answers1

5

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
BENY
  • 317,841
  • 20
  • 164
  • 234
  • 2
    Notice I will recommend keep the first value as NaN since that means not available 0 means different is 0 days that is different – BENY Aug 24 '18 at 00:25
  • I'm getting an `IndexError: unsupported iterator index` error when I'm trying it on my dataset. I'm just looking at a column filled with `np.datetime64` values – madsthaks Aug 24 '18 at 01:36
  • @madsthaks I have no idea where this error come from . – BENY Aug 24 '18 at 02:31