2015-12-15 15 views
6

Quando si converte una matrice NumPy di ​​un frame di dati pandas pandas cambia Uint64 tipi di tipi di oggetto se il numero intero maggiore di 2^63 - 1.Perché i panda convertono int unsigned maggiore di 2 ** 63-1 in oggetti?

import pandas as pd 
import numpy as np 

x = np.array([('foo', 2 ** 63)], dtype = np.dtype([('string', np.str_, 3), ('unsigned', np.uint64)])) 
y = np.array([('foo', 2 ** 63 - 1)], dtype = np.dtype([('string', np.str_, 3), ('unsigned', np.uint64)])) 

print pd.DataFrame(x).dtypes.unsigned 
dtype('O') 
print pd.DataFrame(y).dtypes.unsigned 
dtype('uint64') 

Questo è fastidioso come non riesco a scrivere i dati frame a un file HDF in formato tabella:

pd.DataFrame(x).to_hdf('x.hdf', 'key', format = 'table') 

Ouput:

TypeError: impossibile serializzare colonna [firmato] perché contenuto suoi dati sono [intero] oggetto dtype

Qualcuno può spiegare la conversione del tipo?

+0

È un bug aperto: https://github.com/pydata/pandas/issues/11846#event-492663948 Vedere la mia risposta per un lavoro ar ound. – imp9

risposta

5

E 'un open bug, ma si può forzare di nuovo ad un uint64usingDataFrame.astype()

x = np.array([('foo', 2 ** 63)], dtype = np.dtype([('string', np.str_, 3), ('unsigned', np.uint64)])) 

a = pd.DataFrame(x) 
a['unsigned'] = a['unsigned'].astype(np.uint64) 
>>>a.dtypes 
string  object 
unsigned uint64 
dtype: object 

Altri metodi utilizzati per convertire i tipi di dati per i valori numerici in rilievo gli errori o non funziona:

>>>pd.to_numeric(a['unsigned'], errors = coerce) 
OverflowError: Python int too large to convert to C long 

>>>a.convert_objects(convert_numeric = True).dtypes 
string  object 
unsigned object 
dtype: object 
0
x = np.array([('foo', 2 ** 63)], 
      dtype = np.dtype([('string', np.str_, 3), 
           ('unsigned', 'f4')])) 

y = np.array([('foo', 2 ** 63 - 1)], 
      dtype = np.dtype([('string', np.str_, 3), 
           ('unsigned', 'i8')])) 
+0

Questo cambierà il tipo in un float. – imp9

+0

Aggiungi una spiegazione al tuo codice – EdChum

Problemi correlati