Implements FastAPI backend with ML model support for energy trading, including price prediction models and RL-based battery trading policy. Features dashboard, trading, backtest, and settings API routes with WebSocket support for real-time updates.
36 lines
978 B
Python
36 lines
978 B
Python
import pandas as pd
|
|
|
|
|
|
def add_time_features(df: pd.DataFrame, timestamp_col: str = "timestamp") -> pd.DataFrame:
|
|
result = df.copy()
|
|
|
|
if timestamp_col not in result.columns:
|
|
return result
|
|
|
|
result[timestamp_col] = pd.to_datetime(result[timestamp_col])
|
|
|
|
result["hour"] = result[timestamp_col].dt.hour
|
|
result["day_of_week"] = result[timestamp_col].dt.dayofweek
|
|
result["day_of_month"] = result[timestamp_col].dt.day
|
|
result["month"] = result[timestamp_col].dt.month
|
|
|
|
result["hour_sin"] = _sin_encode(result["hour"], 24)
|
|
result["hour_cos"] = _cos_encode(result["hour"], 24)
|
|
result["day_sin"] = _sin_encode(result["day_of_week"], 7)
|
|
result["day_cos"] = _cos_encode(result["day_of_week"], 7)
|
|
|
|
return result
|
|
|
|
|
|
def _sin_encode(x, period):
|
|
import numpy as np
|
|
return np.sin(2 * np.pi * x / period)
|
|
|
|
|
|
def _cos_encode(x, period):
|
|
import numpy as np
|
|
return np.cos(2 * np.pi * x / period)
|
|
|
|
|
|
__all__ = ["add_time_features"]
|