2016-05-22 21 views
18

gấu trúc nhập 'pandas.indexes' không ném lỗi, nhưng thay vì cố gắng để đọc một gấu trúc dataframe nhặt như vậy:ImportError: Không có mô-đun tên

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') 

Các traceback là 165 dòng với ba trường hợp ngoại lệ đồng thời (bất cứ điều gì có nghĩa là). Có phải read_pickle không tương thích với gấu trúc phiên bản 17.1 tôi đang chạy không? Làm cách nào để bỏ tạm thời khung dữ liệu của tôi để sử dụng?

Dưới đây là một bản sao của traceback:

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' 

Tôi cũng đã thử tải file dưa từ dưa trực tiếp:

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

và có những lỗi tương tự:

--------------------------------------------------------------------------- 
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' 
+2

Tôi nghĩ rằng bạn cần phải tải một phiên bản cũ hơn của gấu trúc để giải quyết vấn đề này. Sau đó lưu dưới dạng csv hoặc cái gì khác. Pickle phụ thuộc vào cấu trúc lớp và mô-đun chỉ để tái tạo lại đối tượng. Nếu cấu trúc này thay đổi, dưa sẽ vỡ. – piRSquared

+7

bạn đang cố gắng đọc một loại dưa chua mới hơn với phiên bản cũ hơn của gấu trúc. pandas.indexes không tồn tại cho đến 0,18.0; gấu trúc cung cấp lại compat không chuyển tiếp compat (có nghĩa là một phiên bản mới hơn có thể đọc cũ hơn, nhưng cũ hơn không thể đọc mới hơn) – Jeff

+2

Tôi đang sử dụng 0.20.2, và có vấn đề này với một pickle đã được tạo ra với một phiên bản cũ. – MPa

Trả lời

20

Tôi có lỗi này khi tôi đã tạo một file PKL với python 2.7 và đã cố gắng để đọc nó với python 3.6 tôi đã làm:

pd.read_pickle('foo.pkl') 

và nó làm việc

+2

Tôi tin rằng đây phải là câu trả lời đúng. Điều này làm việc trong hầu hết các trường hợp, trong khi nâng cấp 'pandas' thì không. –

+0

Tôi cũng đồng ý rằng đây phải là câu trả lời đúng – Kadaj13

9

Tôi gặp sự cố này khi cố gắng mở một khung dữ liệu bị tắt điên e với gấu trúc 0,18,1 sử dụng gấu trúc 0,17,1. Nếu bạn đang sử dụng pip, nâng cấp gấu trúc với:

pip install --upgrade pandas 

Nếu bạn đang sử dụng một thư viện như Anaconda, sử dụng:

conda upgrade pandas 

Nếu bạn cần phải có cả hai phiên bản của gấu trúc trên máy tính của bạn, hãy xem xét sử dụng virtualenv

+0

Làm việc cho tôi. Đó là một chi tiết nhỏ, nhưng tôi đã phải sử dụng pip3 install --upandrade gấu trúc để đảm bảo nó làm việc trên Python 3. – Berun

2

Đây là giải pháp mà không cập nhật gấu trúc hoặc bất kỳ thứ gì bạn đang sử dụng.

Nếu bạn đang sử dụng python2

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

Nếu bạn đang sử dụng python3

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

Giải pháp python3 của bạn không hoạt động với tôi, nó đưa ra một 'UnicodeDecodeError' trên dòng cuối cùng,' 'utf- 8 'codec không thể giải mã byte 0x80 ở vị trí 0: byte bắt đầu không hợp lệ.' – user1416227

+0

vẫn cung cấp cho tôi ModuleNotFoundError: Không có mô-đun có tên' pandas.indexes 'trên python3.6 – Skirrebattie

7

tiết kiệm và tải các phiên bản khác nhau của pandas sử dụng pickle thường không hoạt động. Thay vào đó, hãy sử dụng pandas.HDFStore.

Khi tôi cần cập nhật gấu trúc nhưng cũng cần một số dữ liệu được lưu bằng dưa chuột trong các phiên bản trước, tôi đã quay lại và lưu lại dữ liệu đó ở định dạng HDF, khi không có gì khác hoạt động. Không còn vấn đề nữa.

Làm việc cho bất kỳ loại cấu trúc dữ liệu gấu trúc nào, dường như, ngay cả các khung dữ liệu đa chỉ mục! Trong ngắn hạn, nếu tẩy không thành công sau khi nâng cấp phiên bản, hãy thử HDFStore; nó đáng tin cậy hơn (và hiệu quả hơn!).

+0

Thậm chí không biết về phương pháp này, có lẽ cũng có thể sau đó được mở trong 'R'. – ryanjdillon

Các vấn đề liên quan