Is there an efficient way to compute within-group date differences (in days) for a dataframe in the form:
x = pd.DataFrame(
{'grp':['A','A','A','B','B','B'],
'dt':pd.DatetimeIndex(['1/1/00 00:00:00','1/2/00','1/3/00','1/2/01','1/3/01','1/5/01'])})
x
Out[1]:
dt grp
0 2000-01-01 00:00:00 A
1 2000-01-02 00:00:00 A
2 2000-01-03 00:00:00 A
3 2001-01-02 00:00:00 B
4 2001-01-03 00:00:00 B
5 2001-01-05 00:00:00 B
So that the result is similar to:
grp days_since_start
A 0
A 1
A 2
B 0
B 1
B 3
Sure. Group by the group name, take the smallest time in each group, take the difference:
x.set_index('grp') - x.groupby('grp').min()
# dt
#grp
#A 0 days
#A 1 days
#A 2 days
#B 0 days
#B 1 days
#B 3 days
#Name: dt, dtype: timedelta64[ns]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.