def predict_roll_4hour(roll_time, data): forecast_time = roll_time[0:10] roll_time = datetime.datetime.strptime(roll_time, "%Y-%m-%d %H:%M:%S") day_one = predict_days(forecast_time, 2, data) # 数据划分 predict_sum = day_one.iloc[:, 1] time_gap = int((int(roll_time.minute) + int(roll_time.hour) * 60) / 15) pre_ture_4hour = predict_sum[time_gap: time_gap + 16] time_slots = [] for i in range(16): time_slot = roll_time + datetime.timedelta(minutes=15 * i) time_slots.append(time_slot) df = pd.DataFrame({'date': time_slots[0:len(pre_ture_4hour)], 'load(kw)': pre_ture_4hour}) return df 代码优化
时间: 2024-02-01 07:15:56 浏览: 98
可以考虑使用列表推导式来简化代码:
```python
def predict_roll_4hour(roll_time, data):
forecast_time = roll_time[:10]
roll_time = datetime.datetime.strptime(roll_time, "%Y-%m-%d %H:%M:%S")
day_one = predict_days(forecast_time, 2, data) # 数据划分
predict_sum = day_one.iloc[:, 1]
time_gap = int((int(roll_time.minute) + int(roll_time.hour) * 60) / 15)
pre_ture_4hour = predict_sum[time_gap: time_gap + 16]
time_slots = [roll_time + datetime.timedelta(minutes=15 * i) for i in range(16)]
df = pd.DataFrame({'date': time_slots[:len(pre_ture_4hour)], 'load(kw)': pre_ture_4hour})
return df
```
此外,建议在函数开头加上注释,说明函数的功能和输入输出参数的含义,以增强代码的可读性。
阅读全文