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)]