2015-12-15 77 views
6

當我轉換numpy的陣列到大熊貓數據幀大熊貓改變UINT64類型的對象類型,如果整數大於2^63 - 1爲什麼熊貓將unsigned int大於2 ** 63-1轉換爲對象?

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

這是惱人的,因爲我不能寫入數據幀到表格式的文件HDF:

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

輸出繼電器:

類型錯誤:無法序列列[無符號]因爲 它的數據內容是[整數]對象D型

有人可以解釋類型轉換嗎?

+0

這是一個開放的bug:https://github.com/pydata/pandas/issues/11846#event-492663948 看到我的工作回答ound。 – imp9

回答

5

這是一個open bug,但你可以迫使它回到一個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 

用於將數據類型轉換爲數值出現的錯誤或沒有工作的其他方法:

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

這會將類型更改爲浮點型。 – imp9

+0

請向您的代碼添加說明 – EdChum

相關問題