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

Mac系统Spyder读取Excel为DataFrame时遇TypeError问题求助

Mac系统Spyder中pandas读取Excel报TypeError的解决办法

问题重现

在Mac的Spyder环境下,尝试用pandas读取Excel文件转为DataFrame,初始代码如下:

import pandas as pd
df = pd.read_excel(r"/Users/myusername/Desktop/Test.xlsx")
print(df)

执行后持续抛出以下错误:

Traceback (most recent call last):
  File ~/anaconda3/lib/python3.10/site-packages/IPython/core/async_helpers.py:129 in _pseudo_sync_runner
    coro.send(None)

  File ~/anaconda3/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3260 in run_cell_async
    self.displayhook.exec_result = result

  File ~/anaconda3/lib/python3.10/site-packages/traitlets/traitlets.py:729 in __set__
    self.set(obj, value)

  File ~/anaconda3/lib/python3.10/site-packages/traitlets/traitlets.py:703 in set
    new_value = self._validate(obj, value)

  File ~/anaconda3/lib/python3.10/site-packages/traitlets/traitlets.py:735 in _validate
    value = self.validate(obj, value)

  File ~/anaconda3/lib/python3.10/site-packages/traitlets/traitlets.py:2144 in validate
    if isinstance(value, self.klass):  # type:ignore[arg-type]

TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

ERROR! Session/line number was not unique in database. History logging moved to new session 104

已确认openpyxl已安装,且尝试修改读取参数:

df = pd.read_excel(open('Test.xlsx', 'rb'), sheet_name='Sheet3')

但错误依旧。

解决方案

这个错误并非pandas读取Excel本身的问题,而是IPython/traitlets版本不兼容导致的,具体是traitlets模块的类型校验逻辑出现异常。可以通过以下步骤解决:

  • 升级traitlets到兼容版本
    在终端执行:
pip install --upgrade traitlets==5.9.0

该版本与Python3.10、IPython的兼容性较好。

  • 重启Spyder
    升级完成后,完全关闭Spyder再重新打开,执行读取代码即可正常运行。

  • 备选方案:指定读取引擎
    如果升级后仍有问题,可以在read_excel中明确指定引擎为openpyxl,确保读取逻辑正确:

import pandas as pd
df = pd.read_excel(r"/Users/myusername/Desktop/Test.xlsx", engine='openpyxl')
print(df)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 18:30:20