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

zpf6vheq  于 2022-11-27  发布在  其他
关注(0)|答案(2)|浏览(165)

我想在Pandas数据框中以15分钟的时间间隔将一行拆分为多行6小时的数据

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

我已经尝试了一个小时的时间分割和使用以下代码

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

这对我很有用

result["start_time"][0]

输出量:

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

现在我想要的频率为15分钟的时间间隔,所以它应该给予我24个时间戳

yrefmtwq

yrefmtwq1#

尝试:15T而不是h

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

输出:

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')

如预期-您将获得24个时间戳

gcuhipw9

gcuhipw92#

from datetime import timedelta

df = pd.DataFrame({'start_time': ['2022-08-22 00:15:00'],'end_time': ['2022-08-22 06:15:00']})
df['start_time'] = pd.to_datetime(df['start_time'])
df['end_time'] = pd.to_datetime(df['end_time'])
df['start_time'] = df['start_time'].dt.strftime('%Y-%m-%d %H:%M:%S')
df['end_time'] = df['end_time'].dt.strftime('%Y-%m-%d %H:%M:%S')

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

new_df = pd.date_range(start=df['start_time'][0], end=df['end_time'][0], freq='15min')[:-1]
result_df = pd.DataFrame({'start_time': new_df, 'end_time': new_df + timedelta(minutes=15)})

输出:

> result_df

      start_time            end_time
0  2022-08-22 00:15:00 2022-08-22 00:30:00
1  2022-08-22 00:30:00 2022-08-22 00:45:00
2  2022-08-22 00:45:00 2022-08-22 01:00:00
3  2022-08-22 01:00:00 2022-08-22 01:15:00
4  2022-08-22 01:15:00 2022-08-22 01:30:00
5  2022-08-22 01:30:00 2022-08-22 01:45:00
6  2022-08-22 01:45:00 2022-08-22 02:00:00
7  2022-08-22 02:00:00 2022-08-22 02:15:00
8  2022-08-22 02:15:00 2022-08-22 02:30:00
9  2022-08-22 02:30:00 2022-08-22 02:45:00
10 2022-08-22 02:45:00 2022-08-22 03:00:00
11 2022-08-22 03:00:00 2022-08-22 03:15:00
12 2022-08-22 03:15:00 2022-08-22 03:30:00
13 2022-08-22 03:30:00 2022-08-22 03:45:00
14 2022-08-22 03:45:00 2022-08-22 04:00:00
15 2022-08-22 04:00:00 2022-08-22 04:15:00
16 2022-08-22 04:15:00 2022-08-22 04:30:00
17 2022-08-22 04:30:00 2022-08-22 04:45:00
18 2022-08-22 04:45:00 2022-08-22 05:00:00
19 2022-08-22 05:00:00 2022-08-22 05:15:00
20 2022-08-22 05:15:00 2022-08-22 05:30:00
21 2022-08-22 05:30:00 2022-08-22 05:45:00
22 2022-08-22 05:45:00 2022-08-22 06:00:00
23 2022-08-22 06:00:00 2022-08-22 06:15:00

相关问题