2 回答

TA贡献2021条经验 获得超8个赞
我的五十美分:
首先重新创建示例df:
df = pd.DataFrame({"start_time": [datetime(2018, 8, 23), datetime(2018, 8, 23, 0, 15)],
"end_time": [datetime(2018, 8, 23, 1, 30), datetime(2018, 8, 23, 1, 45)],
"key": ["abcd_eg", "defg_x2"],
"vol": [0.92, 0.27]})
循环开始时间,为每个 start_time 创建一个具有所需索引的新数据帧,并将它们存储在一个列表中。
dfs = []
for row in df.itertuples():
part_df = pd.DataFrame(index=pd.DatetimeIndex(start=row.start_time, end=row.end_time, freq='15T'),
data={'end_time': row.end_time, 'key': row.key, 'vol': row.vol})
part_df.index.name = 'start_time'
dfs.append(part_df)
现在连接所有数据帧并重置索引:
result = pd.concat(dfs).reset_index()
给出以下结果:
start_time end_time key vol
0 2018-08-23 00:00:00 2018-08-23 01:30:00 abcd_eg 0.92
1 2018-08-23 00:15:00 2018-08-23 01:30:00 abcd_eg 0.92
2 2018-08-23 00:30:00 2018-08-23 01:30:00 abcd_eg 0.92
3 2018-08-23 00:45:00 2018-08-23 01:30:00 abcd_eg 0.92
4 2018-08-23 01:00:00 2018-08-23 01:30:00 abcd_eg 0.92
5 2018-08-23 01:15:00 2018-08-23 01:30:00 abcd_eg 0.92
6 2018-08-23 01:30:00 2018-08-23 01:30:00 abcd_eg 0.92
7 2018-08-23 00:15:00 2018-08-23 01:45:00 defg_x2 0.27
8 2018-08-23 00:30:00 2018-08-23 01:45:00 defg_x2 0.27
9 2018-08-23 00:45:00 2018-08-23 01:45:00 defg_x2 0.27
10 2018-08-23 01:00:00 2018-08-23 01:45:00 defg_x2 0.27
11 2018-08-23 01:15:00 2018-08-23 01:45:00 defg_x2 0.27
12 2018-08-23 01:30:00 2018-08-23 01:45:00 defg_x2 0.27
13 2018-08-23 01:45:00 2018-08-23 01:45:00 defg_x2 0.27

TA贡献1796条经验 获得超4个赞
我看到您添加了新信息。也许这就是你要找的,如果 15 分钟间隔的数量是固定的,那么你可以试试这个。Edit2:现在,它也适用于非固定的 15 分钟间隔。
import pandas as pd
gap = '15min'
date_start = ['2018-08-23 00:00:00','2018-08-23 00:15:00','2018-08-24 00:45:00', '2018-08-24 00:30:00']
date_end = ['2018-08-23 01:30:00','2018-08-23 01:45:00','2018-08-24 01:00:00','2018-08-24 02:45:00']
count = 0
to_repeat = []
data = {'start_time':date_start,'end_time':date_end,'key':['abcd_eg','defg_x2', 'whef_98','tuyr_23'],'vol':[0.92,0.27,0.87,0.90]}
df = pd.DataFrame(data)
for _ in zip(date_start, date_end):
temp = pd.date_range(_[0], _[1], freq=gap)
to_repeat.append(len(temp))
if count==0:
ind = temp
else:
ind = ind.append(temp)
count+=1
df_final = df.reindex(df.index.repeat(to_repeat))
df_final['start_time'] = ind
df_final.reset_index(inplace=True)
df_final.drop(columns='index',inplace=True)
print(df_final)
输出
start_time end_time key vol
0 2018-08-23 00:00:00 2018-08-23 01:30:00 abcd_eg 0.92
1 2018-08-23 00:15:00 2018-08-23 01:30:00 abcd_eg 0.92
2 2018-08-23 00:30:00 2018-08-23 01:30:00 abcd_eg 0.92
3 2018-08-23 00:45:00 2018-08-23 01:30:00 abcd_eg 0.92
4 2018-08-23 01:00:00 2018-08-23 01:30:00 abcd_eg 0.92
5 2018-08-23 01:15:00 2018-08-23 01:30:00 abcd_eg 0.92
6 2018-08-23 01:30:00 2018-08-23 01:30:00 abcd_eg 0.92
7 2018-08-23 00:15:00 2018-08-23 01:45:00 defg_x2 0.27
8 2018-08-23 00:30:00 2018-08-23 01:45:00 defg_x2 0.27
9 2018-08-23 00:45:00 2018-08-23 01:45:00 defg_x2 0.27
10 2018-08-23 01:00:00 2018-08-23 01:45:00 defg_x2 0.27
11 2018-08-23 01:15:00 2018-08-23 01:45:00 defg_x2 0.27
12 2018-08-23 01:30:00 2018-08-23 01:45:00 defg_x2 0.27
13 2018-08-23 01:45:00 2018-08-23 01:45:00 defg_x2 0.27
14 2018-08-24 00:45:00 2018-08-24 01:00:00 whef_98 0.87
15 2018-08-24 01:00:00 2018-08-24 01:00:00 whef_98 0.87
16 2018-08-24 00:30:00 2018-08-24 02:45:00 tuyr_23 0.90
17 2018-08-24 00:45:00 2018-08-24 02:45:00 tuyr_23 0.90
18 2018-08-24 01:00:00 2018-08-24 02:45:00 tuyr_23 0.90
19 2018-08-24 01:15:00 2018-08-24 02:45:00 tuyr_23 0.90
20 2018-08-24 01:30:00 2018-08-24 02:45:00 tuyr_23 0.90
21 2018-08-24 01:45:00 2018-08-24 02:45:00 tuyr_23 0.90
22 2018-08-24 02:00:00 2018-08-24 02:45:00 tuyr_23 0.90
23 2018-08-24 02:15:00 2018-08-24 02:45:00 tuyr_23 0.90
24 2018-08-24 02:30:00 2018-08-24 02:45:00 tuyr_23 0.90
25 2018-08-24 02:45:00 2018-08-24 02:45:00 tuyr_23 0.90
添加回答
举报