I have the following data set.
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
I am able to generate the following output
ID Count
abc 3
abc 1
xyz 2
xyz 2
using the following code mentioned in count consecutive days python dataframe
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'])
series = df.groupby('ID').Date.diff().dt.days.ne(1).cumsum()
df.groupby(['ID', series]).size().reset_index(level=1, drop=True)
How can I get the following output?
ID Start End
abc 2017-01-07 2017-01-09
abc 2017-12-09 2017-12-09
xyz 2017-01-05 2017-01-06
xyz 2017-04-15 2017-04-16