From 465feaa8330e0716330edb2018ff48520ad58983 Mon Sep 17 00:00:00 2001 From: fish Date: Thu, 7 May 2026 23:02:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20NaN=20=E5=80=BC=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=20JSON=20=E5=BA=8F=E5=88=97=E5=8C=96=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E3=80=81K=20=E7=BA=BF=E6=95=B0=E6=8D=AE=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.7 --- tushare/src/storage.py | 5 +++++ web/backend/internal/store/futures.go | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tushare/src/storage.py b/tushare/src/storage.py index c3ca757..a348116 100644 --- a/tushare/src/storage.py +++ b/tushare/src/storage.py @@ -68,6 +68,11 @@ def save_candles(df: pd.DataFrame, db_url: str = DEFAULT_DB_URL): df = df.copy() df = df.where(pd.notna(df), None) records = df.to_dict(orient="records") + # pandas float 列的 None 会被转为 NaN,需手动清理后再存入数据库 + for record in records: + for key, val in record.items(): + if isinstance(val, float) and val != val: # NaN != NaN + record[key] = None with conn.cursor() as cur: cur.executemany( """ diff --git a/web/backend/internal/store/futures.go b/web/backend/internal/store/futures.go index da8b100..8fb4480 100644 --- a/web/backend/internal/store/futures.go +++ b/web/backend/internal/store/futures.go @@ -149,9 +149,11 @@ func (s *FuturesStore) ListCandles(tsCode, start, end string) ([]Candle, error) return nil, ErrMissingTsCode } q := `SELECT ts_code, trade_date, - COALESCE(open, 0), COALESCE(high, 0), COALESCE(low, 0), COALESCE(close, 0), - COALESCE(vol, 0), COALESCE(amount, 0), - COALESCE(oi, 0), COALESCE(oi_chg, 0), COALESCE(pre_close, 0) + COALESCE(NULLIF(open, 'NaN'::real), 0), COALESCE(NULLIF(high, 'NaN'::real), 0), + COALESCE(NULLIF(low, 'NaN'::real), 0), COALESCE(NULLIF(close, 'NaN'::real), 0), + COALESCE(NULLIF(vol, 'NaN'::real), 0), COALESCE(NULLIF(amount, 'NaN'::real), 0), + COALESCE(NULLIF(oi, 'NaN'::real), 0), COALESCE(NULLIF(oi_chg, 'NaN'::real), 0), + COALESCE(NULLIF(pre_close, 'NaN'::real), 0) FROM candles WHERE ts_code = $1` args := []any{tsCode} n := 1