From 9697b59d4f3dbd777069e32f2e9be8e8492e5fc0 Mon Sep 17 00:00:00 2001 From: shellway-pc <413209390@qq.com> Date: Tue, 11 Aug 2026 21:51:48 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?=E8=B4=A2=E5=8A=A1=E7=B1=BB=E6=95=B0=E6=8D=AENAN=E5=80=BC?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E7=9A=84bug=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quantitative_data/importer.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/quantitative_data/importer.py b/quantitative_data/importer.py index 4433d98..a2fc43e 100644 --- a/quantitative_data/importer.py +++ b/quantitative_data/importer.py @@ -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)]