Если возможно, при работе с таймделтами сначала используйте to_timedelta
, затем Series.dt.floor
и если по модулю 15 меньше или равно 10, удалите 15 минут:
d = {'Time': ['08:00:00', '08:01:00', '08:02:00', '08:03:00', '08:04:00',
'08:05:00', '08:06:00', '08:07:00', '08:08:00', '08:09:00',
'08:10:00', '08:11:00', '08:12:00', '08:13:00', '08:14:00',
'08:15:00', '08:16:00', '08:17:00', '08:18:00', '08:19:00',
'08:20:00', '08:21:00', '08:22:00', '08:23:00', '08:24:00',
'08:25:00', '08:26:00', '08:27:00', '08:28:00', '08:29:00',
'08:30:00', '08:31:00', '08:32:00', '08:33:00', '08:34:00',
'08:35:00', '08:36:00', '08:37:00', '08:38:00', '08:39:00']}
df = pd.DataFrame(d)
df['Time'] = pd.to_timedelta(df['Time'])
s = df['Time'].dt.floor(freq='15T')
#https://stackoverflow.com/a/14190143 for convert timedeltas to minutes
df['new'] = np.where(((df['Time'].dt.total_seconds() % 3600) // 60) % 15 <= 10,
s - pd.Timedelta(15 * 60, 's'), s)
print (df)
Time new
0 08:00:00 07:45:00
1 08:01:00 07:45:00
...
9 08:09:00 07:45:00
10 08:10:00 07:45:00
11 08:11:00 08:00:00
12 08:12:00 08:00:00
...
24 08:24:00 08:00:00
25 08:25:00 08:00:00
26 08:26:00 08:15:00
27 08:27:00 08:15:00
...
38 08:38:00 08:15:00
39 08:39:00 08:15:00
При необходимости работа с датами и временем аналогична Series.dt.minute
:
df = pd.DataFrame({'Time':pd.date_range('2015-01-01 08:00:00', freq='T', periods=40)})
s = df['Time'].dt.floor(freq='15T')
df['new'] = np.where(df['Time'].dt.minute % 15 <= 10, s - pd.Timedelta(15*60, 's'), s)
print (df)
Time new
0 2015-01-01 08:00:00 2015-01-01 07:45:00
1 2015-01-01 08:01:00 2015-01-01 07:45:00
...
9 2015-01-01 08:09:00 2015-01-01 07:45:00
10 2015-01-01 08:10:00 2015-01-01 07:45:00
11 2015-01-01 08:11:00 2015-01-01 08:00:00
12 2015-01-01 08:12:00 2015-01-01 08:00:00
13 2015-01-01 08:13:00 2015-01-01 08:00:00
...
24 2015-01-01 08:24:00 2015-01-01 08:00:00
25 2015-01-01 08:25:00 2015-01-01 08:00:00
26 2015-01-01 08:26:00 2015-01-01 08:15:00
27 2015-01-01 08:27:00 2015-01-01 08:15:00
...
38 2015-01-01 08:38:00 2015-01-01 08:15:00
39 2015-01-01 08:39:00 2015-01-01 08:15:00
Альтернативное решение из комментария:
df['new1'] = df['Time'].sub(pd.Timedelta(11*60, 's')).dt.floor(freq='15T')