I want to make a table that has a time range for each id in a list, I have the following code that does it correctly but I think it is not the most efficient or pythonic, is there a better way to do it?
My code:
ids = [1, 2, 3, 4, 5]
df = pd.DataFrame()
for id in ids:
df_aux = pd.DataFrame(
{'init': pd.date_range(start='2022-04-06 10:00:00', end='2022-04-06 15:00:00', freq="H"),
"color": "#FF0000",
"id": id}
)
df_aux['end'] = df_aux['init'] + pd.Timedelta(seconds=3600)
df = pd.concat([df,df_aux], ignore_index=True)
Correct result:
fecha_inicio color id fecha_fin
0 2022-04-06 10:00:00 #FF0000 1 2022-04-06 11:00:00
1 2022-04-06 11:00:00 #FF0000 1 2022-04-06 12:00:00
2 2022-04-06 12:00:00 #FF0000 1 2022-04-06 13:00:00
3 2022-04-06 13:00:00 #FF0000 1 2022-04-06 14:00:00
4 2022-04-06 14:00:00 #FF0000 1 2022-04-06 15:00:00
5 2022-04-06 15:00:00 #FF0000 1 2022-04-06 16:00:00
6 2022-04-06 10:00:00 #FF0000 2 2022-04-06 11:00:00
7 2022-04-06 11:00:00 #FF0000 2 2022-04-06 12:00:00
8 2022-04-06 12:00:00 #FF0000 2 2022-04-06 13:00:00
9 2022-04-06 13:00:00 #FF0000 2 2022-04-06 14:00:00
10 2022-04-06 14:00:00 #FF0000 2 2022-04-06 15:00:00
11 2022-04-06 15:00:00 #FF0000 2 2022-04-06 16:00:00
12 2022-04-06 10:00:00 #FF0000 3 2022-04-06 11:00:00
13 2022-04-06 11:00:00 #FF0000 3 2022-04-06 12:00:00
14 2022-04-06 12:00:00 #FF0000 3 2022-04-06 13:00:00
15 2022-04-06 13:00:00 #FF0000 3 2022-04-06 14:00:00
16 2022-04-06 14:00:00 #FF0000 3 2022-04-06 15:00:00
17 2022-04-06 15:00:00 #FF0000 3 2022-04-06 16:00:00
18 2022-04-06 10:00:00 #FF0000 4 2022-04-06 11:00:00
19 2022-04-06 11:00:00 #FF0000 4 2022-04-06 12:00:00
20 2022-04-06 12:00:00 #FF0000 4 2022-04-06 13:00:00
21 2022-04-06 13:00:00 #FF0000 4 2022-04-06 14:00:00
22 2022-04-06 14:00:00 #FF0000 4 2022-04-06 15:00:00
23 2022-04-06 15:00:00 #FF0000 4 2022-04-06 16:00:00
24 2022-04-06 10:00:00 #FF0000 5 2022-04-06 11:00:00
25 2022-04-06 11:00:00 #FF0000 5 2022-04-06 12:00:00
26 2022-04-06 12:00:00 #FF0000 5 2022-04-06 13:00:00
27 2022-04-06 13:00:00 #FF0000 5 2022-04-06 14:00:00
28 2022-04-06 14:00:00 #FF0000 5 2022-04-06 15:00:00
29 2022-04-06 15:00:00 #FF0000 5 2022-04-06 16:00:00