我的数据框如下:
time1-high
0 18008 days 00:00:04
1 18008 days 00:00:11
2 18008 days 00:32:44
3 18008 days 01:16:19
4 18008 days 02:28:50
5 18008 days 02:30:37
6 18008 days 04:48:37
7 18008 days 09:57:19
8 18008 days 10:19:30
我想要的是以下内容:
time1-high
0 18008 days 00:00:15
1 18008 days 00:00:15
2 18008 days 00:32:45
3 18008 days 01:16:30
4 18008 days 02:29:00
5 18008 days 02:30:45
6 18008 days 04:48:45
7 18008 days 09:57:30
8 18008 days 10:19:30
dtype: timedelta64[ns]
该想法是将秒修改为15
的最接近倍数。因此,在第一行0 18008 days 00:00:04
中,应将其更改为0 18008 days 00:00:15
,4 18008 days 02:28:50
到4 18008 days 02:29:00
等等……我对此有些困惑。是否有可以自动执行此功能的功能?谢谢!
答案 0 :(得分:2)
将Series.dt.ceil
与15S
一起使用:
df['new1'] = df['time1-high'].dt.ceil(freq='15S')
print (df)
time1-high new1
0 18008 days 00:00:04 18008 days 00:00:15
1 18008 days 00:00:11 18008 days 00:00:15
2 18008 days 00:32:44 18008 days 00:32:45
3 18008 days 01:16:19 18008 days 01:16:30
4 18008 days 02:28:50 18008 days 02:29:00
5 18008 days 02:30:37 18008 days 02:30:45
6 18008 days 04:48:37 18008 days 04:48:45
7 18008 days 09:57:19 18008 days 09:57:30
8 18008 days 10:19:30 18008 days 10:19:30