python - Finding Date difference in list of dates [Pandas] -
assuming following dataset has sorted list of dates:
dates=pd.dataframe(data={'client':['1','2'], 'date':[['2012-3-10','2012-3-11','2012-3-12','2012-3-13','2012-3-14'], ['2012-3-12','2012-3-13','2012-3-16','2012-3-23']]}) i want find average date difference in terms of days so, eg, client '2', average timelag 2.75
starting with:
client date 0 1 [2012-3-10, 2012-3-11, 2012-3-12, 2012-3-13, 2... 1 2 [2012-3-12, 2012-3-13, 2012-3-16, 2012-3-23] you could
dates.groupby('client')['date'].apply(lambda x: [i / np.timedelta64(1, 'd') in np.diff([pd.to_datetime(c) c in x])[0]]) to timedelta in days:
client 1 [1.0, 1.0, 1.0, 1.0] 2 [1.0, 3.0, 7.0] or
dates.groupby('client')['date'].apply(lambda x: np.mean([i / np.timedelta64(1, 'd') in np.diff([pd.to_datetime(c) c in x])[0]])) for mean:
client 1 1.000000 2 3.666667 
Comments
Post a Comment