You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于15分钟间隔在Pandas DataFrame中将一行拆分为6小时多行数据

Pandas DataFrame按15分钟时间间隔拆分单行数据

现有如下单行DataFrame数据,需要将其按15分钟间隔拆分为覆盖6小时的多行数据(最终应得到24个时间戳):

start_time          end_time    
0   2022-08-22 00:15:00 2022-08-22 06:15:00

此前已实现按1小时间隔拆分的代码如下:

result['start_time'] = result.apply(lambda d: pd.date_range(d['start_time'],
                                                    d['end_time'], 
                                                    freq='h')[:-1], 
                            axis=1) 

修改方案

要改成15分钟间隔,只需将freq参数的值从'h'(小时)改为'15min'(或'15T',T是Pandas中分钟的缩写)即可,同时保留[:-1]来排除结束时间(原end_time为6:15,和start_time间隔6小时,15分钟步长下,从00:15到06:00正好生成24个时间戳)。

修改后的完整代码:

result['start_time'] = result.apply(lambda d: pd.date_range(d['start_time'],
                                                    d['end_time'], 
                                                    freq='15min')[:-1], 
                            axis=1) 

执行结果

运行后得到的DatetimeIndex如下:

DatetimeIndex(['2022-08-22 00:15:00', '2022-08-22 00:30:00',
               '2022-08-22 00:45:00', '2022-08-22 01:00:00',
               '2022-08-22 01:15:00', '2022-08-22 01:30:00',
               '2022-08-22 01:45:00', '2022-08-22 02:00:00',
               '2022-08-22 02:15:00', '2022-08-22 02:30:00',
               '2022-08-22 02:45:00', '2022-08-22 03:00:00',
               '2022-08-22 03:15:00', '2022-08-22 03:30:00',
               '2022-08-22 03:45:00', '2022-08-22 04:00:00',
               '2022-08-22 04:15:00', '2022-08-22 04:30:00',
               '2022-08-22 04:45:00', '2022-08-22 05:00:00',
               '2022-08-22 05:15:00', '2022-08-22 05:30:00',
               '2022-08-22 05:45:00', '2022-08-22 06:00:00'],
              dtype='datetime64[ns]', freq='15T')

内容的提问来源于stack exchange,提问作者Rajan Kumar Yadav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 22:01:09