numpy 从Pandas Dataframe 中的周期性事件创建IntervalIndex

5ktev3wc  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(113)

我有一个 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"]

它给出了两个独立的系列;startend,其中包含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'
wlsrxk51

wlsrxk511#

你试过这样的东西吗?

df['duration'] = pd.to_timedelta(df['duration'])
ef = pd.DataFrame() # new df

# loop through
for i, row in df.iterrows():
    # date range for the given window start and end dates with duration as frequency
    dates = pd.date_range(row['window_start'], row['window_end'], freq=row['duration'])
    event_df = pd.DataFrame({'start': dates, 'end': dates + row['duration'], 'id': i+1})
    # append
    ef = ef.append(event_df)

# resample dataframe by id
result = ef.set_index('start').resample('D')['id'].count()

相关问题