2015-12-15 6 views
6

Kiedy konwertować numpy tablicę do pand ramka danych pandy zmienia uint64 rodzajów typów obiektów, jeżeli całkowita jest większa niż 2^63 - 1.Dlaczego pandy konwertują unsigned int większe niż 2 ** 63-1 na obiekty?

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

Jest to uciążliwe, ponieważ nie może zapisać dane rama do pliku hDF formatu tabeli:

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

wyjściami:

Błąd typu: nie może szeregować kolumnę [znaku] ponieważ zawartość nim dane są [całkowita] przedmiotem dtype

Czy ktoś może wyjaśnić konwersję typu?

+0

To jest otwarty błąd: https://github.com/pydata/pandas/issues/11846#event-492663948 Zobacz moją odpowiedź na pracę lub ound. – imp9

Odpowiedz

5

To open bug, ale można zmusić go do 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 

Inne metody stosowane do konwersji typów danych dla wartości liczbowych podniesionymi błędy lub nie działa:

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

Spowoduje to zmianę typu na float. – imp9

+0

Proszę dodać wyjaśnienie do kodu – EdChum

Powiązane problemy