Python -- read_pickle 导入错误:没有名为 index.base 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36888228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python -- read_pickle ImportError: No module named indexes.base
提问by Sapling
I write a numeric dataframe to .pkl file in a machine (df.to_pickle()), for some reason, I have to open this file in a different machine (pd.read_pickle()), I get an Import Error saying: No module named indexes.base, and when I try to import indexes, there doesn't seem to have one.
我将一个数字数据帧写入一台机器 (df.to_pickle()) 中的 .pkl 文件,出于某种原因,我必须在另一台机器上打开这个文件 (pd.read_pickle()),我收到一个导入错误说:不名为 index.base 的模块,当我尝试导入索引时,似乎没有。
When I tried to_csv in a machine and read_csv in a different one, it worked.
当我在一台机器上尝试 to_csv 并在另一台机器上 read_csv 时,它起作用了。
Many Thanks!
非常感谢!
ImportError Traceback (most recent call last)
<ipython-input-199-2be4778e3b0a> in <module>()
----> 1 pd.read_pickle("test.pkl")
C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\io\pickle.pyc in read_pickle(path)
58
59 try:
---> 60 return try_read(path)
61 except:
62 if PY3:
C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\io\pickle.pyc in try_read(path, encoding)
55 except:
56 with open(path, 'rb') as fh:
---> 57 return pc.load(fh, encoding=encoding, compat=True)
58
59 try:
C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\compat\pickle_compat.pyc in load(fh, encoding, compat, is_verbose)
114 up.is_verbose = is_verbose
115
--> 116 return up.load()
117 except:
118 raise
C:\Users\AppData\Local\Continuum\Anaconda2\lib\pickle.pyc in load(self)
856 while 1:
857 key = read(1)
--> 858 dispatch[key](self)
859 except _Stop, stopinst:
860 return stopinst.value
C:\Users\AppData\Local\Continuum\Anaconda2\lib\pickle.pyc in load_global(self)
1088 module = self.readline()[:-1]
1089 name = self.readline()[:-1]
--> 1090 klass = self.find_class(module, name)
1091 self.append(klass)
1092 dispatch[GLOBAL] = load_global
C:\Users\AppData\Local\Continuum\Anaconda2\lib\pickle.pyc in find_class(self, module, name)
1122 def find_class(self, module, name):
1123 # Subclasses may override this
--> 1124 __import__(module)
1125 mod = sys.modules[module]
1126 klass = getattr(mod, name)
ImportError: No module named indexes.base
回答by pmaniyan
This error can be caused by a version mismatch between the version of pandas used to save the dataframe and the version of pandas used to load it.
此错误可能是由用于保存数据帧的 Pandas 版本与用于加载它的 Pandas 版本之间的版本不匹配引起的。
Please check the Python and Pandas version in both the machines.
请检查两台机器中的 Python 和 Pandas 版本。
Also, if versions are same, can you please share the dataframe that you used with to_pickle(), so that we can look into it.
另外,如果版本相同,能否请您分享您与 to_pickle() 一起使用的数据帧,以便我们进行调查。
回答by elz
Using pd.read_pickle
can also help backwards compatibility if you are trying to read a dataframe only. See github issue.
pd.read_pickle
如果您仅尝试读取数据帧,使用还有助于向后兼容。请参阅github 问题。
I unfortunately have a dictionary of dataframes, so am going to try to use a virtual environment with an older version to load, re-save only the dataframes, and then use pd.read_pickle
.
不幸的是,我有一个数据帧字典,因此我将尝试使用具有旧版本的虚拟环境来加载,仅重新保存数据帧,然后使用pd.read_pickle
.