Rohit Lamba :
I have a dataframe with a column which have time data in the format HH:MM:SS. Sample data is shown below for reference:
Time
09:25:03
09:28:40
09:36:12
09:36:14
09:41:10
09:51:00
09:58:48
10:00:11
10:00:17
10:21:44
10:21:53
10:32:58
11:08:59
11:45:55
11:49:14
12:18:54
12:21:22
13:05:47
13:19:37
13:19:57
13:25:22
14:21:10
I want to get the nearest time previous to current time which is divisible by 5. I want the output like below:
Time Nearest_Time
09:25:03 09:25:00
09:28:40 09:25:00
09:36:12 09:35:00
09:36:14 09:35:00
09:41:10 09:40:00
09:51:00 09:50:00
09:58:48 09:50:00
10:00:11 10:00:00
10:00:17 10:00:00
10:21:44 10:20:00
10:21:53 10:20:00
10:32:58 10:30:00
11:08:59 11:05:00
11:45:55 11:45:00
11:49:14 11:45:00
12:18:54 12:15:00
12:21:22 12:20:00
13:05:47 13:05:00
13:19:37 13:15:00
13:19:57 13:15:00
13:25:22 13:25:00
14:21:10 14:20:00
yatu :
You can use dt.floor
setting freq
to 5 minutes:
pd.to_datetime(df.Time).dt.floor('5 min')
0 2020-02-14 09:25:00
1 2020-02-14 09:25:00
2 2020-02-14 09:35:00
3 2020-02-14 09:35:00
4 2020-02-14 09:40:00
5 2020-02-14 09:50:00
6 2020-02-14 09:55:00
7 2020-02-14 10:00:00
8 2020-02-14 10:00:00
9 2020-02-14 10:20:00
10 2020-02-14 10:20:00
11 2020-02-14 10:30:00
12 2020-02-14 11:05:00
13 2020-02-14 11:45:00
14 2020-02-14 11:45:00
15 2020-02-14 12:15:00
16 2020-02-14 12:20:00
17 2020-02-14 13:05:00
18 2020-02-14 13:15:00
19 2020-02-14 13:15:00
20 2020-02-14 13:25:00
21 2020-02-14 14:20:00
Name: Time, dtype: datetime64[ns]