gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:12 28 4
gpt4 key购买 nike

我通过 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com