From 817580ffe84be946050a6e2b459458c68ec573e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AE=97=E5=B9=B3?= Date: Thu, 11 Jun 2026 22:17:27 +0800 Subject: [PATCH] fix: drop NaN timestamps, use facecolor to suppress matplotlib warning --- ts_anomaly_td/io_csv.py | 9 +++++++++ ts_anomaly_td/visualize.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ts_anomaly_td/io_csv.py b/ts_anomaly_td/io_csv.py index 9223114..2872a2f 100644 --- a/ts_anomaly_td/io_csv.py +++ b/ts_anomaly_td/io_csv.py @@ -41,6 +41,15 @@ def read_csv(path: str | Path, value_col: str = "value") -> tuple[list[int], lis fixed = raw_strs.apply(_fix_hour60) timestamps.loc[na_mask] = pd.to_datetime(fixed, errors="coerce") + # 丢弃时间戳仍为 NaN 的行(coerce 后无法恢复的异常时间戳) + valid_mask = timestamps.notna() + if not valid_mask.all(): + n_drop = (~valid_mask).sum() + import logging + logging.getLogger(__name__).warning("dropping %d rows with invalid timestamps", n_drop) + timestamps = timestamps[valid_mask].reset_index(drop=True) + df = df[valid_mask].reset_index(drop=True) + timestamps_ms = (timestamps.astype("int64") // 10**6).tolist() values = df[value_col].astype(float).tolist() diff --git a/ts_anomaly_td/visualize.py b/ts_anomaly_td/visualize.py index 7fdbaea..ae6a446 100644 --- a/ts_anomaly_td/visualize.py +++ b/ts_anomaly_td/visualize.py @@ -291,7 +291,7 @@ def render_column( ax.plot(hours, values, color="#333333", linewidth=0.8, alpha=0.9) if has_gt: for start_h, end_h in gt_windows_h: - ax.axvspan(start_h, end_h, alpha=0.35, color="#FF4444", + ax.axvspan(start_h, end_h, alpha=0.35, facecolor="#FF4444", edgecolor="#CC0000", linewidth=1.5) # 异常点散点标记 anom_hours = [hours[i] for i, l in enumerate(gt_labels) if l == 1] @@ -326,7 +326,7 @@ def render_column( for wstart, wend in windows: ws_h = (wstart - t0) / 3_600_000 we_h = (wend - t0) / 3_600_000 - ax.axvspan(ws_h, we_h, alpha=0.4, color=color, + ax.axvspan(ws_h, we_h, alpha=0.4, facecolor=color, edgecolor=color, linewidth=1.5) det_hours_list.append((ws_h, we_h))