ImportError:没有名为'pandas.indexes'的模块

时间:2016-05-22 06:56:17

标签: python numpy pandas pickle

导入pandas并没有抛出错误,而是试图读取拾取的pandas数据帧:

import numpy as np
import pandas as pd
import matplotlib
import seaborn as sns
sns.set(style="white")

control_data = pd.read_pickle('null_report.pickle')
test_data = pd.read_pickle('test_report.pickle')

回溯是165行,有三个并发异常(无论这意味着什么)。 read_pickle与pandas版本17.1不兼容我正在运行吗?如何取消我的数据帧的使用?

以下是追溯的副本:

ImportError                               Traceback (most recent call last)
C:\Users\test\Anaconda3\lib\site-packages\pandas\io\pickle.py in try_read(path, encoding)
     45             with open(path, 'rb') as fh:
---> 46                 return pkl.load(fh)
     47         except (Exception) as e:

ImportError: No module named 'pandas.indexes'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
C:\Users\test\Anaconda3\lib\site-packages\pandas\io\pickle.py in try_read(path, encoding)
     51                 with open(path, 'rb') as fh:
---> 52                     return pc.load(fh, encoding=encoding, compat=False)
     53 

C:\Users\test\Anaconda3\lib\site-packages\pandas\compat\pickle_compat.py in load(fh, encoding, compat, is_verbose)
    115 
--> 116         return up.load()
    117     except:

C:\Users\test\Anaconda3\lib\pickle.py in load(self)
   1038                 assert isinstance(key, bytes_types)
-> 1039                 dispatch[key[0]](self)
   1040         except _Stop as stopinst:

C:\Users\test\Anaconda3\lib\pickle.py in load_stack_global(self)
   1342             raise UnpicklingError("STACK_GLOBAL requires str")
-> 1343         self.append(self.find_class(module, name))
   1344     dispatch[STACK_GLOBAL[0]] = load_stack_global

C:\Users\test\Anaconda3\lib\pickle.py in find_class(self, module, name)
   1383                 module = _compat_pickle.IMPORT_MAPPING[module]
-> 1384         __import__(module, level=0)
   1385         if self.proto >= 4:

ImportError: No module named 'pandas.indexes'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
C:\Users\test\Anaconda3\lib\site-packages\pandas\io\pickle.py in read_pickle(path)
     59     try:
---> 60         return try_read(path)
     61     except:

C:\Users\test\Anaconda3\lib\site-packages\pandas\io\pickle.py in try_read(path, encoding)
     56                 with open(path, 'rb') as fh:
---> 57                     return pc.load(fh, encoding=encoding, compat=True)
     58 

C:\Users\test\Anaconda3\lib\site-packages\pandas\compat\pickle_compat.py in load(fh, encoding, compat, is_verbose)
    115 
--> 116         return up.load()
    117     except:

C:\Users\test\Anaconda3\lib\pickle.py in load(self)
   1038                 assert isinstance(key, bytes_types)
-> 1039                 dispatch[key[0]](self)
   1040         except _Stop as stopinst:

C:\Users\test\Anaconda3\lib\pickle.py in load_stack_global(self)
   1342             raise UnpicklingError("STACK_GLOBAL requires str")
-> 1343         self.append(self.find_class(module, name))
   1344     dispatch[STACK_GLOBAL[0]] = load_stack_global

C:\Users\test\Anaconda3\lib\pickle.py in find_class(self, module, name)
   1383                 module = _compat_pickle.IMPORT_MAPPING[module]
-> 1384         __import__(module, level=0)
   1385         if self.proto >= 4:

ImportError: No module named 'pandas.indexes'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
C:\Users\test\Anaconda3\lib\site-packages\pandas\io\pickle.py in try_read(path, encoding)
     45             with open(path, 'rb') as fh:
---> 46                 return pkl.load(fh)
     47         except (Exception) as e:

ImportError: No module named 'pandas.indexes'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
C:\Users\test\Anaconda3\lib\site-packages\pandas\io\pickle.py in try_read(path, encoding)
     51                 with open(path, 'rb') as fh:
---> 52                     return pc.load(fh, encoding=encoding, compat=False)
     53 

C:\Users\test\Anaconda3\lib\site-packages\pandas\compat\pickle_compat.py in load(fh, encoding, compat, is_verbose)
    115 
--> 116         return up.load()
    117     except:

C:\Users\test\Anaconda3\lib\pickle.py in load(self)
   1038                 assert isinstance(key, bytes_types)
-> 1039                 dispatch[key[0]](self)
   1040         except _Stop as stopinst:

C:\Users\test\Anaconda3\lib\pickle.py in load_stack_global(self)
   1342             raise UnpicklingError("STACK_GLOBAL requires str")
-> 1343         self.append(self.find_class(module, name))
   1344     dispatch[STACK_GLOBAL[0]] = load_stack_global

C:\Users\test\Anaconda3\lib\pickle.py in find_class(self, module, name)
   1383                 module = _compat_pickle.IMPORT_MAPPING[module]
-> 1384         __import__(module, level=0)
   1385         if self.proto >= 4:

ImportError: No module named 'pandas.indexes'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-17-3b05fe7d20a4> in <module>()
      3 # test_data = np.genfromtxt(fh, usecols=2)
      4 
----> 5 control_data = pd.read_pickle('null_report.pickle')
      6 test_data = pd.read_pickle('test_report.pickle')
      7 

C:\Users\test\Anaconda3\lib\site-packages\pandas\io\pickle.py in read_pickle(path)
     61     except:
     62         if PY3:
---> 63             return try_read(path, encoding='latin1')
     64         raise

C:\Users\test\Anaconda3\lib\site-packages\pandas\io\pickle.py 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\test\Anaconda3\lib\site-packages\pandas\compat\pickle_compat.py in load(fh, encoding, compat, is_verbose)
    114         up.is_verbose = is_verbose
    115 
--> 116         return up.load()
    117     except:
    118         raise

C:\Users\test\Anaconda3\lib\pickle.py in load(self)
   1037                     raise EOFError
   1038                 assert isinstance(key, bytes_types)
-> 1039                 dispatch[key[0]](self)
   1040         except _Stop as stopinst:
   1041             return stopinst.value

C:\Users\test\Anaconda3\lib\pickle.py in load_stack_global(self)
   1341         if type(name) is not str or type(module) is not str:
   1342             raise UnpicklingError("STACK_GLOBAL requires str")
-> 1343         self.append(self.find_class(module, name))
   1344     dispatch[STACK_GLOBAL[0]] = load_stack_global
   1345 

C:\Users\test\Anaconda3\lib\pickle.py in find_class(self, module, name)
   1382             elif module in _compat_pickle.IMPORT_MAPPING:
   1383                 module = _compat_pickle.IMPORT_MAPPING[module]
-> 1384         __import__(module, level=0)
   1385         if self.proto >= 4:
   1386             return _getattribute(sys.modules[module], name)[0]

ImportError: No module named 'pandas.indexes'

我也尝试直接从pickle加载pickle文件:

via_pickle = pickle.load( open( 'null_report.pickle', "rb" ) )

并得到了同样的错误:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-23-ba2e3adae1c4> in <module>()
      1 
----> 2 via_pickle = pickle.load( open( 'null_report.pickle', "rb" ) )
      3 
      4 # control_data = pd.read_pickle('null_report.pickle')
      5 # test_data = pd.read_pickle('test_report.pickle')

ImportError: No module named 'pandas.indexes'

7 个答案:

答案 0 :(得分:43)

当我使用python 2.7创建一个pkl文件并尝试使用python 3.6读取它时,我遇到了这个错误 我做了:

pd.read_pickle('foo.pkl')

并且有效

答案 1 :(得分:15)

我尝试使用pandas 0.17.1打开使用pandas 0.18.1制作的pickle数据帧时出现此问题。 如果您使用pip,请使用以下命令升级pandas:

pip install --upgrade pandas

如果您使用的是像anaconda这样的库,请使用:

conda upgrade pandas

如果您的计算机上需要同时拥有两个版本的pandas,请考虑使用virtualenv

答案 2 :(得分:9)

使用bif b"June 5, 2017" in webPageCopy: print ('success') 的不同版本中保存和加载通常不起作用。相反,请使用pandas.HDFStore

当我需要更新pandas但还需要在以前的版本中使用pickle保存的一些数据时,我回去并以HDF格式重新保存这些数据,而没有其他任何工作。没问题了。

适用于任何类型的pandas数据结构,甚至是多索引数据帧!简而言之,如果版本升级后酸洗失败,请尝试HDFStore;它更可靠(也更有效率!)。

答案 3 :(得分:3)

这是没有更新熊猫或任何使用的解决方案。

如果你正在使用python2

import pickle
with open('filename.pkl', 'rb') as fo:
        dict = pickle.load(fo, encoding='latin1’)

如果你正在使用python3

import _pickle as cPickle
with open('filename.pkl', 'rb') as fo:
        dict = cPickle.load(fo, encoding='latin1’)

答案 4 :(得分:1)

在熊猫0.23.4中,有一种更好的方法可以解决此问题。使用pandas.read_pickle来读取文件对象,例如:

pd.read_pickle(open('test_report.pickle', 'rb'))

答案 5 :(得分:0)

如果您要阅读腌制的文本而不是文件,请执行

  <script>
            $('#fileUpload').click(function (e) {
                if ($('#file').val() === "") {
                    Swal.fire({
                        title: 'Nothing',
                        text: 'No file selected',
                        type: 'error',
                        confirmButtonText: 'Again!!',
                        timer: 4000

                    })
                }
                else {
                    Swal.fire({
                        title: 'Wait awhile...',
                        text: 'File will be uploaded shortly',
                        type: 'success',
                        confirmButtonText: 'Okay, cool',
                        timer: 4000
                    })
                }

            });
        </script>

如果遇到错误-import io pd.read_pickle(io.BytesIO(pickled_text))
明确提到压缩类型。它可以是None(无压缩),gzip,bz2,xz或zip(取决于文件扩展名)之一。
ValueError: Unrecognized compression type: infer

答案 6 :(得分:0)

处理内部API更改(可能会破坏解锁)的灵活方式是实现自定义Unpickler实例。

例如,pandas.indexes模块已移至pandas.core.indexes。我们可以编写一个Unpickler,它会相应地调整模块路径。为此,我们可以覆盖方法find_class

import sys
class Unpickler(pickle.Unpickler):
    def find_class(self, module, name):
        '''This method gets called for every module pickle tries to load.'''
        # python 2 --> 3 compatibility: __builtin__ has been renamed to builtins
        if module == '__builtin__':
            module = 'builtins'
        # pandas compatibility: in newer versions, pandas.indexes has been moved to pandas.core.indexes
        if 'pandas.indexes' in module:
            module = module.replace('pandas.indexes', 'pandas.core.indexes')
        __import__(module)
        return getattr(sys.modules[module], name)

with open('/path/to/pickle.pkl', 'rb') as file:
    pdf = Unpickler(file).load()