You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法使用pandas.DataFrame.to_gbq上传DataFrame至BigQuery求助

问题分析与解决方案

这个ConversionError的核心原因是数据类型与BigQuery schema不兼容,导致Parquet转换失败,以下是具体修复方案和排查步骤:

1. 修复NUMERIC字段类型不匹配

BigQuery的NUMERIC类型对应高精度十进制数,你代码中revenue列用的是pandas默认的float64类型,在Parquet转换时无法直接映射。需要将该列转为Decimal类型:

from decimal import Decimal
import pandas as pd

table_schema = [
    {'name':'company','type':'STRING'},
    {'name':'country','type':'STRING'},
    {'name':'revenue','type':'NUMERIC'},
    {'name':'pageviews','type':'INTEGER'}
]

# 将revenue值改为Decimal类型,匹配BigQuery NUMERIC要求
dataframe = pd.DataFrame({
    'company': ['stackoverflow'],
    'country': ['france'],
    'revenue': [Decimal('1000.23')],
    'pageviews': [10000]
})

table_name = 'my_dataset.petit_test'

dataframe.to_gbq(
    table_name,
    'PROJECT_ID',
    chunksize=100000,
    if_exists='replace',
    table_schema=table_schema
)

2. 更新依赖库

旧版本的pandas-gbq或pyarrow(Parquet处理核心依赖)可能存在转换bug,执行以下命令更新到最新兼容版本:

pip install --upgrade pandas pandas-gbq pyarrow

3. 临时验证(可选)

如果需要快速定位问题,可以临时将schema中的revenue类型改为FLOAT64,如果能成功运行,就确认是NUMERIC与float类型的映射冲突导致的转换失败。


内容的提问来源于stack exchange,提问作者JCDB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 07:52:34