2016-05-22 31 views
18

panda Importazione di non buttare l'errore, ma piuttosto cercando di leggere un panda dataframe scelto come tale:ImportError: No module named 'pandas.indexes'

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

Il traceback è di 165 linee con tre eccezioni simultanei (Qualsiasi cosa significhi). read_pickle non compatibile con la versione 17.1 di panda Sono in esecuzione? Come faccio a disimpegnare il mio dataframe per l'uso?

Qui di seguito è una copia del 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' 

Ho anche provato il caricamento del file salamoia da Pickle direttamente:

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

e ottenuto lo stesso errore:

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

Penso che avete bisogno di caricare un versione precedente di panda per annullarla. Quindi salva come CSV o qualcos'altro. Pickle dipende dalla struttura della classe e dal modulo che punta a ricostruire l'oggetto. Se questa struttura cambia, il sottaceto si spezzerà. – piRSquared

+7

stai cercando di leggere un sottaceto più recente con una versione precedente di panda. pandas.indexes non esiste fino a 0.18.0; panda offre di nuovo compat non forward compat (cioè una versione più recente può leggere più vecchio, ma più vecchio non può leggere più recente) – Jeff

+2

Sto usando 0.20.2, e ho questo problema con un pickle che è stato generato con una versione precedente. – MPa

risposta

20

ho avuto questo errore quando ho creato un file PKL con Python 2.7 e stava cercando di leggerlo con Python 3.6 ho fatto:

pd.read_pickle('foo.pkl') 

e ha funzionato

+2

Credo che questa dovrebbe essere la risposta corretta. Questo funziona nella maggior parte dei casi, mentre l'aggiornamento di 'pandas' no. –

+0

Sono anche d'accordo che questa sia la risposta corretta – Kadaj13

9

Ho avuto questo problema provando ad aprire un dataframe decapitato pazzo e con panda 0.18.1 usando panda 0.17.1. Se si utilizza pip, l'aggiornamento panda con:

pip install --upgrade pandas 

Se si utilizza una libreria come Anaconda, l'uso:

conda upgrade pandas 

Se avete bisogno di avere entrambe le versioni del panda sulla vostra macchina, prendere in considerazione utilizzando virtualenv

+0

ha funzionato per me. È un piccolo dettaglio, ma ho dovuto usare pip3 install --upgrade pandas per assicurarmi che funzionasse su Python 3. – Berun

2

Ecco la soluzione senza aggiornare panda o qualsiasi altra cosa si utilizza.

Se stai usando python2

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

Se stai usando python3

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

La tua soluzione python3 non funziona per me, dà un 'UnicodeDecodeError' sull'ultima riga,' 'utf- Il codec 8 'non può decodificare il byte 0x80 in posizione 0: byte iniziale non valido. – user1416227

+0

mi dà ancora ModuleNotFoundError: nessun modulo chiamato' pandas.indexes 'su python3.6 – Skirrebattie

7

Salvataggio e caricamento in diverse versioni di pandas usando pickle spesso non funziona. Invece, utilizzare pandas.HDFStore.

Quando ho avuto bisogno di aggiornare i panda ma avevo anche bisogno di alcuni dati salvati con pickle nelle versioni precedenti, sono tornato indietro e ho ri-salvato quei dati in formato HDF, invece, quando nient'altro avrebbe funzionato. Nessun problema più.

Funziona per qualsiasi tipo di struttura dei dati dei panda che sembra, anche i dataframes multi-indicizzati! In breve, se il pickling fallisce dopo un aggiornamento della versione, prova HDFStore; è più affidabile (e più efficiente!).

+0

Non conoscevo nemmeno questo metodo, che potrebbe forse anche quindi essere aperto in 'R'. – ryanjdillon