I have a dataframe df with at column date which consists of date.
If I want to calculate the maximum difference between the dates within each group, is that doable (without having to re-group and without using .apply)? If I do
df
id | date |
---+-------
1 | 2020-01-20
1 | 2020-01-25
2 | 2020-02-03
2 | 2020-02-04
max_diff_for_each_id = df.groupby("id").diff(1).max()
max_diff_for_each_id
id
--
1 5
that of course give the maximum difference between all groups, where I want
id
--
1 5
2 1
I know I can just re-group max_diff_for_each_id but I think that
max_diff_for_each_id = df.groupby("id").diff(1).groupby("id").max()
is not really "pretty" and say you have multiple functions to apply, there is a ton of overhead by having to re-group all the time