Skip to main content
 首页 » 编程设计

Python pickle 错误 : TypeError: object pickle not returning list

2025年05月04日24thcjp

我知道这个非常古老的问题 Python pickling error: TypeError: object pickle not returning list. Issue with numpy?但给出的唯一答案相当模糊。

这是在 Python 3.6 中重现问题的代码。

import pickle 
from astroquery.irsa import Irsa 
from astropy import units as u 
 
# Query region. 
table = Irsa.query_region("m31", catalog="fp_psc", spatial="Cone", 
                          radius=.5 * u.arcmin) 
 
# Dump table. 
with open('table.pkl', 'wb') as f: 
    pickle.dump(table, f) 
 
# This is where the issue appears. 
with open('table.pkl', 'rb') as f: 
    table = pickle.load(f) 

当尝试加载 pickled 数据时,它失败了:

Traceback (most recent call last): 
  File "/home/gabriel/Descargas/test.py", line 17, in <module> 
    table2 = pickle.load(f) 
  File "/home/gabriel/anaconda3/envs/cat-match/lib/python3.6/site-packages/astropy/table/column.py", line 238, in __setstate__ 
    super_class.__setstate__(self, state) 
  File "/home/gabriel/anaconda3/envs/cat-match/lib/python3.6/site-packages/numpy/ma/core.py", line 5869, in __setstate__ 
    super(MaskedArray, self).__setstate__((shp, typ, isf, raw)) 
TypeError: object pickle not returning list 

我该如何解决这个问题?


使用 conda您可以使用以下命令在 Python 3 环境中安装要求:

conda install astropy 
conda install -c astropy astroquery 

请您参考如下方法:

链接的答案提到 numpy 掩码数组在解包时会导致麻烦。考虑到 astropy 表可能确实有屏蔽条目(并非每个对象或位置都可能有一个值,例如,每个波段或任何相关列),这确实可能是您问题的根源:屏蔽数组。

numpy 问题技巧也显示了这个问题:Masked array with object dtype doesn't unpickle .这似乎在 pull request 8122 中得到了解决.

搜索release notes for Numpy 1.12.0在那里显示这个拉取请求。因此,除非您已经在使用 numpy 1.12,否则升级 numpy 可能是值得的。