Very similar to @BradSolomon's answer, with two small differences:
df.sort_values(['item', 'date']).groupby('item')['date'].agg(
lambda g: g.diff().mean() / pd.Timedelta(days=1))
# gives:
item
A 54.0
B 65.5
C 39.0
Notes:
- ensure that dates are sorted within each group, otherwise the mean will depend on the order; in your example, the dates happen to be sorted, so if you can guarantee it, you may skip
.sort_values()
;
- use
... / pd.Timedelta(days=1)
to produce directly the mean difference in units of days.
Alternative for speed (no sort, no lambda, but a bit more opaque)
gb = df.groupby('item')['date']
(gb.max() - gb.min()) / (gb.count() - 1) / pd.Timedelta(days=1)
# gives:
item
A 54.0
B 65.5
C 39.0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…