feat: add --value-col param to inject for multi-column CSV support

This commit is contained in:
张宗平
2026-06-11 21:14:01 +08:00
parent 8859fd8835
commit 45fc062627
2 changed files with 6 additions and 5 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ def cmd_inject(args):
"""inject 子命令:读 CSV → 建表 → 批量插入。"""
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
ts, vals, labels = read_csv(args.csv)
ts, vals, labels = read_csv(args.csv, value_col=args.value_col)
rows = list(zip(ts, vals, labels))
logger.info("read %d rows from %s", len(rows), args.csv)
+5 -4
View File
@@ -4,11 +4,12 @@ from pathlib import Path
import pandas as pd
def read_csv(path: str | Path) -> tuple[list[int], list[float], list[int]]:
def read_csv(path: str | Path, value_col: str = "value") -> tuple[list[int], list[float], list[int]]:
"""读 timestamp,value,label CSV 文件。
Args:
path: CSV 文件路径
value_col: 数值列名 (default: "value")
Returns:
(timestamps_ms, values, labels) 三元组
@@ -22,13 +23,13 @@ def read_csv(path: str | Path) -> tuple[list[int], list[float], list[int]]:
"""
df = pd.read_csv(path)
if "timestamp" not in df.columns or "value" not in df.columns:
raise ValueError(f"CSV 缺少 timestamp 或 value 列: {path}")
if "timestamp" not in df.columns or value_col not in df.columns:
raise ValueError(f"CSV 缺少 timestamp 或 {value_col} 列: {path}")
timestamps = pd.to_datetime(df["timestamp"])
timestamps_ms = (timestamps.astype("int64") // 10**6).tolist()
values = df["value"].astype(float).tolist()
values = df[value_col].astype(float).tolist()
labels = df["label"].fillna(0).astype(int).tolist() if "label" in df.columns else [0] * len(df)