检索自上次操作以来的时间差 -- python/pandas

Retrieve time difference since last action -- python/pandas

假设我有包含购买和时间两个字段的购买记录。

我想要得到的是自第一次不购买以来经过的第三列时间,它看起来像:

buy| time | time difference
1  | 8:00 | NULL
0  | 9:01 | NULL
0  | 9:10 | NULL
0  | 9:21 | NULL
1  | 9:31 | 0:30
0  | 9:41 | NULL
0  | 9:42 | NULL
1  | 9:53 | 0:12

我怎样才能做到这一点?在我看来,它是 pd.groupby() 和 pd.shift() 的混合,但我似乎无法在脑海中解决这个问题。

IIUC

df.time=pd.to_datetime(df.time)


df.loc[df.buy==1,'DIFF']=df.groupby(df.buy.cumsum().shift().fillna(0)).time.transform(lambda x : x.iloc[-1]-x.iloc[0])
df
Out[19]: 
   buy                time timedifference     DIFF
0    1 2018-02-26 08:00:00            NaN 00:00:00
1    0 2018-02-26 09:01:00            NaN      NaT
2    0 2018-02-26 09:10:00            NaN      NaT
3    0 2018-02-26 09:21:00            NaN      NaT
4    1 2018-02-26 09:31:00           0:30 00:30:00
5    0 2018-02-26 09:41:00            NaN      NaT
6    0 2018-02-26 09:42:00            NaN      NaT
7    1 2018-02-26 09:53:00           0:12 00:12:00

#df.buy.cumsum().shift().fillna(0) Create the key for groupby 

#time.transform(lambda x : x.iloc[-1]-x.iloc[0]) create the different for each group
#df.loc[df.buy==1,'DIFF'] fill the value from groupby by the right position which buy equal to 1