1.需求
⭐️ 在构造时间序列数据时,最后构造好的numpy时间序列数据,无法构造成DataFrame,报错
2. 解决办法
⭐️ 通过字典进行构造
3.源数据
⭐️ 以下是原始数据,总共112行
4.构造
4.1构造代码
⭐️ 时间窗口设置为20,构造代码如下
all_time_seq = e['time_seq'].values
all_request_head_tokens = e['request_head_tokens'].values
test_dict = {"pre_token":[], "all_time":[]}
all_pre_token = np.empty((0,300))
all_time = np.empty((0,20,3))
total_len = len(e)
for i in range(total_len):
if i == 0:
all_pre_token = np.append(all_pre_token, [np.ones(300)], axis=0)
test_dict["pre_token"].append(np.ones(300))
if i > 0:
all_pre_token = np.append(all_pre_token, [all_request_head_tokens[i-1]], axis=0)
test_dict["pre_token"].append(all_request_head_tokens[i-1])
if i < 19:
temp_time = np.array([25, 61, 61]*(19-i) + [x for x1 in list(all_time_seq[:i+1]) for x in x1]).reshape(20,3)
all_time = np.append(all_time, [temp_time], axis=0)
test_dict["all_time"].append(temp_time)
if i >= 19:
temp_time = np.array([x for x1 in list(all_time_seq[i-19:i+1]) for x in x1]).reshape(20,3)
all_time = np.append(all_time, [temp_time], axis=0)
test_dict["all_time"].append(temp_time)
print(all_pre_token.shape)
print(all_time.shape)
testdf = pd.DataFrame(test_dict)
testdf.index = e.index
testdf
4.2构造结果
⭐️ 构造了时分秒时间序列,窗口为20,所以每条数据的shape为 20*3,总共112行
⭐️ 查看构造的尺寸
print(testdf.iloc[0,1].shape)
# 输出为
# (20, 3)
4.3 拼接源数据
⭐️ 拼接代码
e = pd.concat([e, testdf], axis=1)
⭐️ 拼接结果