Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count consecutive days python dataframe

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?

like image 257
primo7 Avatar asked Oct 23 '18 21:10

primo7


1 Answers

Create a Series which checks for the difference between Dates within each ID. Check if that's not 1 day, and then groupby the ID and the cumulative sum of that Series.

import pandas as pd

s = df.groupby('ID').Date.diff().dt.days.ne(1).cumsum()
df.groupby(['ID', s]).size().reset_index(level=1, drop=True)

Output:

ID
abc    3
abc    1
xyz    2
xyz    2
dtype: int64
like image 79
ALollz Avatar answered Oct 13 '22 13:10

ALollz