我知道这个非常老的问题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)


尝试加载腌制的数据时,失败并显示:

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掩码数组,这些数组在取消拾取时会引起麻烦。考虑到天花乱麻的表可能确实具有被屏蔽的条目(并非每个对象或位置可能都有一个值,例如每个带或任何相关列),这确实可能是问题的根源:被屏蔽的数组。

numpy问题骗子也显示了该问题:Masked array with object dtype doesn't unpickle。看来可以在pull request 8122中解决。

清理release notes for Numpy 1.12.0会在其中显示此拉取请求。因此,除非您已经在使用numpy 1.12,否则可能值得升级numpy。

10-08 13:12
查看更多