Skip to main content
 首页 » 编程设计

python之为什么 Pandas 在读取 pickle 文件时会尝试导入模块

2025年05月04日16qlqwjy

我通过 Instagram API 收集了一些数据,我将这些数据存储到 pandas DataFrame 中,而后者又通过 pandas .to_pickle() 方法保存。

当尝试使用 `read_pickle()' 方法在另一台计算机上加载 DataFrame 时,返回以下错误:

Traceback (most recent call last): 
File "examine.py", line 14, in <module> 
dataframe = pd.read_pickle(args["dataframe"]) 
File "/home/user/virtualenvs/geopandas/local/lib/python2.7/site-packages/pandas/io/pickle.py", line 65, in read_pickle 
return try_read(path) 
File "/home/user/virtualenvs/geopandas/local/lib/python2.7/site-packages/pandas/io/pickle.py", line 62, in try_read 
return pc.load(fh, encoding=encoding, compat=True) 
File "/home/user/virtualenvs/geopandas/local/lib/python2.7/site-packages/pandas/compat/pickle_compat.py", line 117, in load 
return up.load() 
File "/usr/lib/python2.7/pickle.py", line 858, in load 
dispatch[key](self) 
File "/usr/lib/python2.7/pickle.py", line 1090, in load_global 
klass = self.find_class(module, name) 
File "/usr/lib/python2.7/pickle.py", line 1124, in find_class 
__import__(module) 
ImportError: No module named instagram.models 

知道是什么原因造成的吗?

请您参考如下方法:

Pickle 根本不知道如何重新创建类。一个类如何被 unpickled 和恢复的信息存储在类内部:__new__、__init__、__setstate__ 等等。

Similarly, when class instances are pickled, their class’s code and data are not pickled along with them. Only the instance data are pickled. This is done on purpose, so you can fix bugs in a class or add methods to the class and still load objects that were created with an earlier version of the class. If you plan to have long-lived objects that will see many versions of a class, it may be worthwhile to put a version number in the objects so that suitable conversions can be made by the class’s __setstate__() method.

来源:Python pickle: What can be pickled and unpickled?

因此,要对其进行 unpickle,pickle 需要加载该类(以及任何中间模块)。

如果您没有/想要 instagram-模块,您应该检查如何将原始数据框中的适当值转换为普通类(int,float, array, ...) 在你 pickle 之前。