修复 NaN 值导致 JSON 序列化失败、K 线数据无法展示
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -68,6 +68,11 @@ def save_candles(df: pd.DataFrame, db_url: str = DEFAULT_DB_URL):
|
|||||||
df = df.copy()
|
df = df.copy()
|
||||||
df = df.where(pd.notna(df), None)
|
df = df.where(pd.notna(df), None)
|
||||||
records = df.to_dict(orient="records")
|
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:
|
with conn.cursor() as cur:
|
||||||
cur.executemany(
|
cur.executemany(
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -149,9 +149,11 @@ func (s *FuturesStore) ListCandles(tsCode, start, end string) ([]Candle, error)
|
|||||||
return nil, ErrMissingTsCode
|
return nil, ErrMissingTsCode
|
||||||
}
|
}
|
||||||
q := `SELECT ts_code, trade_date,
|
q := `SELECT ts_code, trade_date,
|
||||||
COALESCE(open, 0), COALESCE(high, 0), COALESCE(low, 0), COALESCE(close, 0),
|
COALESCE(NULLIF(open, 'NaN'::real), 0), COALESCE(NULLIF(high, 'NaN'::real), 0),
|
||||||
COALESCE(vol, 0), COALESCE(amount, 0),
|
COALESCE(NULLIF(low, 'NaN'::real), 0), COALESCE(NULLIF(close, 'NaN'::real), 0),
|
||||||
COALESCE(oi, 0), COALESCE(oi_chg, 0), COALESCE(pre_close, 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`
|
FROM candles WHERE ts_code = $1`
|
||||||
args := []any{tsCode}
|
args := []any{tsCode}
|
||||||
n := 1
|
n := 1
|
||||||
|
|||||||
Reference in New Issue
Block a user