Python — read_pickle ImportError: No module named

2019-04-03 02:46发布

问题:

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.

When I tried to_csv in a machine and read_csv in a different one, it worked.

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

回答1:

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.

Please check the Python and Pandas version in both the machines.

Also, if versions are same, can you please share the dataframe that you used with to_pickle(), so that we can look into it.



回答2:

Using pd.read_pickle can also help backwards compatibility if you are trying to read a dataframe only. See github issue.

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.