pandas groupby date select earliest per day

Viewed 3351

I have the following dataset:

            value            timestamp
0            Fire  2017-10-03 14:33:52
1           Water  2017-10-04 14:33:48
2            Fire  2017-10-04 14:33:45
3            Fire  2017-10-05 14:33:30
4           Water  2017-10-03 14:33:40
5           Water  2017-10-05 14:32:13
6           Water  2017-10-04 14:32:01
7            Fire  2017-10-03 14:31:55

I want to group this set by timestamp per day and then only select the earliest row per day. For the above example the following should be the result:

            value            timestamp
1           Water  2017-10-05 14:32:13
2           Water  2017-10-04 14:32:01
3            Fire  2017-10-03 14:31:55

For example, for the day 2017-10-03 there are 3 entries but I only want the earliest on that day.

4 Answers

If you have unique index, you can use idxmin on timestamp to find out the indices of the minimum timestamp and extract them with loc:

df.timestamp = pd.to_datetime(df.timestamp)
df.loc[df.groupby(df.timestamp.dt.date, as_index=False).timestamp.idxmin()]

#   value             timestamp
#7   Fire   2017-10-03 14:31:55
#6  Water   2017-10-04 14:32:01
#5  Water   2017-10-05 14:32:13

Just Making Sure

df.timestamp = pd.to_datetime(df.timestamp)

Solution

d1 = df.sort_values('timestamp')
d1[~d1.timestamp.dt.date.duplicated()]

   value           timestamp
7   Fire 2017-10-03 14:31:55
6  Water 2017-10-04 14:32:01
5  Water 2017-10-05 14:32:13

Use dt.floor and head:

df.sort_values('timestamp').groupby(df['timestamp'].dt.floor('D')).head(1)

Output:

   value           timestamp
7   Fire 2017-10-03 14:31:55
6  Water 2017-10-04 14:32:01
5  Water 2017-10-05 14:32:13

Or

df.groupby(df.timestamp.dt.date).apply(lambda x:x[x.timestamp==min(x.timestamp)])
Out[714]: 
              value           timestamp
timestamp                              
2017-10-03 7   Fire 2017-10-03 14:31:55
2017-10-04 6  Water 2017-10-04 14:32:01
2017-10-05 5  Water 2017-10-05 14:32:13
Related