I want to add dates and days that are contained in a column after grouping by an ID column.
The following generates an example df:
df = pd.DataFrame(
{
"ID":[1,1,1,1,2,2,2,3,3,3,3,3,3],
"Date":list(pd.date_range("2018-1-1", "2018-4-10", periods=4)) + list(pd.date_range("2018-6-6", "2018-7-30", periods=3)) + list(pd.date_range("2018-1-1", "2020-1-1", periods=6))
}
)
df['date_intervals'] = df.groupby('ID').Date.diff()
df['new_date_intermediate'] = df.date_intervals.mask(pd.isnull, df['Date'])
Grouped by the ID field, I want a cumulative sum returning dates.
For example, for ID = 1, I want a vector of the first row + the second row, this would be 2018-01-01 + 33 days, followed by the result of that sum plus the third row which is adding another 33 days.
