我有一个 Dataframe ,如下所示:
duration,window_start,window_end,REPETITIONS
0 days 01:00:00,2023-12-31,2024-01-07,5
0 days 00:30:00,2021-10-28,2021-11-02,10
0 days 00:20:00,2022-12-24,2023-01-04,15
0 days 01:00:00,2023-06-15,2023-06-17,20
我希望将这些周期性事件提取到一个 Dataframe 中,该 Dataframe 包含基于REPETITIONS的数量以及window_start和window_end的开始时间和结束时间。在上面的示例中,应该有5+10+15+20=50个离散事件。我正在努力将此转换矢量化,并且没有找到一种方法来循环遍历每一行。
目前为止我得到的信息是:
import pandas as pd
import numpy as np
periodic = pd.read_csv("events.csv",header=0,parse_dates=["start_date", "end_date"], index_col="id")
start = periodic.apply(lambda row: np.linspace(row["window_start"].value, row["window_end"].value, row["REPETITIONS"]), axis=1)
start = start.apply(lambda row: pd.to_datetime(row))
end = start + periodic["duration"]
它给出了两个独立的系列;start和end,其中包含Series中每个 id 的DateTimeIndex,即:
start.head()
1,"DatetimeIndex([ '2021-12-31 00:00:00',
'2022-01-01 00:01:00',
'2021-01-01 00:02:00',
'2021-01-01 00:03:00',
end.head()
1,"DatetimeIndex([ '2021-12-31 01:00:00',
'2022-01-01 00:02:00',
'2021-01-01 00:03:00',
'2021-01-01 00:04:00',
目标是得到如下所示的结果:
id, start, end
1,'2021-12-31 00:00:00','2021-12-31 00:01:00'
1,'2021-12-31 00:00:00','2021-12-31 00:01:00'
1,'2021-12-31 00:00:00','2021-12-31 00:01:00'
.
.
.
2,'2021-10-28 00:00:00','2021-10-28 00:30:00'
2,'2021-10-28 13:20:00','2021-10-28 13:50:00'
1条答案
按热度按时间wlsrxk511#
你试过这样的东西吗?