How to count the number of rows in a given time interval in python pandas? -
i have pandas dataframe numer of columns contain timestamps events can happen objects, object ids index rows.
obj_id | event1 | event2 | event3 | ... 1 | datetime| datetime | nat | ... ... | ... | ... | ... | ...
i want count number of occurences of event on course of day (discarding date), in intervals specify.
sor far, solve recunstructing number of minutes since midnight using datetime.hour
, datetime.minute
:
i = 5 # number of minutes in interval i'm interested in ev1_counts = df.groupby( df.event1.apply(lambda x: * ((60*x.hour + x.minute)//i)) )['event1'].count()
this job, seems unpythonic , i'm sure there better way. how?
i have seen this question, trying
time_series = pd.datetimeindex(df.event1) ts_df = pd.series([1]*len(time_series), index=time_series) ev1_counts = ts_df.groupby(pd.timegrouper(freq = '{:d}min'.format(i)).count()
keeps date information, want discard. converting pd.datetime
objects .time()
method seems problematic, since result can not treated datetime object.
it seems can omit apply
, simplify solution to:
ev1_counts = df.groupby((60*df.event1.dt.hour+df.event1.dt.minute)//i * i)['event1'].count()
Comments
Post a Comment