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

标签 python pandas pickle instagram-api

我通过 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-模块,您应该检查如何将原始数据框中的适当值转换为普通类(intfloat, array, ...) 在你 pickle 之前。

关于python - 为什么 Pandas 在读取 pickle 文件时会尝试导入模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41570417/

相关文章:

python - python 中 Map() 的固定参数

python 与 R 的 Feather 与字符串的兼容性

python - 为什么 pandas.DataFrame.mean() 有效,但 pandas.DataFrame.std() 不能处理相同的数据

python - Altair pandas 值计数水平条形图

python - 防止字段被 pickle

python - wxpython:EVT_GRID_CELL_CHANGED 问题

python对相似的分类值进行分组

python - 如何在 Python 3 中 pickle os.environ?

python - 如何在 "stop"和 "resume"长时间运行Python脚本?

python - 如何删除 pandas.read_csv 中的索引(自动生成)