Python 为什么熊猫会将大于2**63-1的无符号整数转换为对象?

Python 为什么熊猫会将大于2**63-1的无符号整数转换为对象?,python,numpy,pandas,pytables,Python,Numpy,Pandas,Pytables,当我将numpy数组转换为pandas数据帧时,如果整数大于2^63-1,pandas会将uint64类型更改为对象类型 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

当我将numpy数组转换为pandas数据帧时,如果整数大于2^63-1,pandas会将uint64类型更改为对象类型

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')
输出:

TypeError:无法序列化列[未签名],因为 其数据内容为[integer]对象数据类型

有人能解释一下类型转换吗?

这是一个,但您可以强制它返回到
uint64
DataFrame.astype()

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')]))
用于将数据类型转换为数值的其他方法引发错误或不起作用:

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

这会将类型更改为float。这是一个开放的bug:请参阅我的答案以了解解决方法。
>>>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