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

通过API上传Pandas DataFrame至Google Sheets新工作表报错求助

问题排查:DataFrame上传Google Sheets的警告与新工作表操作问题

问题描述

此前用Python结合Pandas、gspread、df2gspread库通过API上传DataFrame到Google Sheets一切正常,但尝试将新DataFrame上传至新工作表时出现警告,需排查原因。

相关代码

import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from df2gspread import df2gspread as d2g

# 凭证文件路径
path_to_credential = 'путь.json'

# Google表格名称
table_name = 'py-to-google'

# 权限范围
scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name(path_to_credential, scope)

gs = gspread.authorize(credentials)
work_sheet = gs.open(table_name)

# 选择第一个工作表
sheet1 = work_sheet.sheet1

# 获取数据(列表格式)
data = sheet1.get_all_values()

# 提取表头
headers = data.pop(0)

# 创建DataFrame
df = pd.DataFrame(data, columns=headers)

spreadsheet_name = 'py-to-google'
sheet = 'py-to-google'

# 上传DataFrame
d2g.upload(df, spreadsheet_name, sheet, credentials=credentials, row_names=True)

运行警告信息(中文翻译)

/var/folders/wv/j__1xb_j3bjctrgyln3pnt7m0000gn/T/ipykernel_5022/1142499611.py:3: 弃用警告:[已弃用][6.0.0版本生效]:client_factory将被gspread.http_client类型替代
  d2g.upload(df, spreadsheet_name, sheet, credentials=credentials, row_names=True)
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/df2gspread/df2gspread.py:138: 未来警告:DataFrame.applymap已被弃用,请使用DataFrame.map替代。
  df = df.applymap(str)
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/df2gspread/df2gspread.py:138: 未来警告:DataFrame.applymap已被弃用,请使用DataFrame.map替代。
  df = df.applymap(str)

<Worksheet 'py-to-google' id:2069214263>

原因分析与解决方法

警告原因解析

  1. 弃用警告(DeprecationWarning):df2gspread库的参数机制即将更新,client_factory参数会被替换为gspread的http_client类型,当前通过credentials直接传参的方式在未来版本可能失效。
  2. 未来警告(FutureWarning):Pandas 2.1.0版本开始弃用applymap方法,但df2gspread库内部仍在使用该方法,导致兼容性警告。

新工作表上传的代码问题

原代码实际是读取已有工作表的数据生成DataFrame,再上传到同名工作表——这并非“新DataFrame上传新工作表”的操作。若要实现目标,需调整代码逻辑。

修复方案

  1. 临时处理警告:

    • 若功能正常,可暂时忽略警告;
    • 降级Pandas到2.1.0以下版本,或等待df2gspread库更新适配Pandas新方法;
    • 升级df2gspread到最新版本,查看是否已修复client_factory的弃用问题。
  2. 正确实现新DataFrame上传新工作表:
    直接准备新的DataFrame,指定新的工作表名称(不存在时会自动创建),示例代码如下:

import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from df2gspread import df2gspread as d2g

path_to_credential = 'путь.json'
spreadsheet_name = 'py-to-google'
# 指定新工作表名称
new_sheet = '新工作表'

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name(path_to_credential, scope)

# 准备你的新DataFrame(替换为实际数据)
new_df = pd.DataFrame({
    'ID': [1, 2, 3],
    '名称': ['测试1', '测试2', '测试3'],
    '值': [100, 200, 300]
})

# 上传到新工作表
d2g.upload(new_df, spreadsheet_name, new_sheet, credentials=credentials, row_names=True)
  1. 权限检查:确保服务账号对目标Google表格拥有编辑权限,且权限范围包含drive和spreadsheets相关权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 15:45:06