I'm trying to group IDs by consecutive dates.
ID Date
abc 2017-01-07
abc 2017-01-08
abc 2017-01-09
abc 2017-12-09
xyz 2017-01-05
xyz 2017-01-06
xyz 2017-04-15
xyz 2017-04-16
Need to return:
ID Count
abc 3
abc 1
xyz 2
xyz 2
I've tried:
d = {'ID': ['abc', 'abc', 'abc', 'abc', 'xyz', 'xyz', 'xyz', 'xyz'], 'Date': ['2017-01-07','2017-01-08', '2017-01-09', '2017-12-09', '2017-01-05', '2017-01-06', '2017-04-15', '2017-04-16']}
df = pd.DataFrame(data=d)
df['Date'] = pd.to_datetime(df['Date'])
today = pd.to_datetime('2018-10-23')
x = df.sort_values('Date', ascending=0)
g = x.groupby(['ID'])
x[(today - x['Date']).dt.days == g.cumcount()].groupby(['ID']).size()
Is there a simple way to do this in order to obtain the counts of all date ranges by ID?