Why does pandas convert unsigned int greater than 2**63-1 to objects?

怎甘沉沦 提交于 2019-12-04 06:05:27

It's an open bug, but you can force it back to an uint64 using DataFrame.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

Other methods used to convert data types to numeric values raised errors or did not work:

>>>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
Echo Baker
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')]))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!