fix:修复了财务类数据NAN值报错的bug。

This commit is contained in:
2026-08-11 21:51:48 +08:00
parent 4e7f599274
commit 9697b59d4f
+8 -2
View File
@@ -200,8 +200,14 @@ def batch_insert(table_name: str, df: pd.DataFrame, conn, conflict_columns: List
f"已保留最新公告记录 (去重后 {after_dedup} 行)"
)
# pd.NaT / numpy NaN 等无法被 psycopg2 识别,统一替换为 Python None
df = df.where(pd.notna(df), None)
# pd.NaT / numpy NaN 等无法被 psycopg2 识别,统一替换为 Python None
# 关键:必须先 astype(object)pandas 的 datetime64 列无法存储 None
# 直接 where(..., None) 时 pandas 会把 None 自动提升回 NaT
# 导致 psycopg2 生成 'NaT'::timestamp 非法 SQL
# (典型报错: fina_indicator 的 ann_date 为空时报
# "invalid input syntax for type timestamp: \"NaT\"")。
# 转为 object 后 None 可正常存储,NaT/NaN 会被真正替换为 NULL。
df = df.astype(object).where(pd.notna(df), None)
rows = [tuple(row) for row in df.itertuples(index=False)]