我有以下数据框:
ID time
0 12 2017-09-17 15:30:00
1 13 2017-09-24 18:00:00
我想在每个时间值前 1h 和后 2小时添加频率,
ID time
0 12 2017-09-17 14:30:00
1 12 2017-09-17 14:31:00
2 12 2017-09-17 14:32:00
3 12 2017-09-17 14:33:00
...
59 12 2017-09-17 15:29:00
60 12 2017-09-17 15:30:00
...
179 12 2017-09-17 17:29:00
180 12 2017-09-17 17:30:00
181 13 2017-09-24 17:00:00
...
有人知道如何生成这些偏移量吗?
答案 0 :(得分:1)
这应该有效。
import pandas as pd
# Sample data
data = pd.DataFrame({
"ID": ["ID1", "ID2"],
"time": ["2017-09-17 15:30:00", "2017-09-24 18:00:00"]
})
data['time'] = pd.to_datetime(data['time'])
print(data.head())
ID time
0 ID1 2017-09-17 15:30:00
1 ID2 2017-09-24 18:00:00
# Logic
ID = []
time = []
for idx, row in data.iterrows():
tm = row['time']
split = pd.date_range(
tm - pd.DateOffset(hours=1),
tm + pd.DateOffset(hours=2),
freq="1min"
)
val = [row['ID']] * len(split)
ID.extend(val)
time.extend(split)
# Result
df = pd.DataFrame({"ID": ID, "time": time})
print(df.head(20))
ID time
0 ID1 2017-09-17 14:30:00
1 ID1 2017-09-17 14:31:00
2 ID1 2017-09-17 14:32:00
3 ID1 2017-09-17 14:33:00
4 ID1 2017-09-17 14:34:00
5 ID1 2017-09-17 14:35:00
.
.
.
答案 1 :(得分:0)
您可以将time
设置为索引并reindex
:
df.time = pd.to_datetime(df.time)
df.set_index('time', inplace=True)
new_index = pd.concat(pd.date_range(end=a,freq='T', periods=61)
.to_series() for a in df.index)
new_df = df.reindex(new_index).bfill().reset_index()
输出(new_df.head()
):
index ID
0 2017-09-17 14:30:00 12.0
1 2017-09-17 14:31:00 12.0
2 2017-09-17 14:32:00 12.0
3 2017-09-17 14:33:00 12.0
4 2017-09-17 14:34:00 12.0