numpy:使用column_stack时是否可以保留列的dtype

fet*_*lke 6 python numpy

当我column_stack用来连接NumPy数组时,dtype转换为:

a = numpy.array([1., 2., 3.], dtype=numpy.float64)
b = numpy.array([1, 2, 3], dtype=numpy.int64)
print numpy.column_stack((a, b)).dtype
>>> float64
Run Code Online (Sandbox Code Playgroud)

有没有办法保留dtype各列?

Dal*_*lek 3

您可以使用方法堆叠两个数组numpy.lib.recfunctions并使用它保留类型:

>>> from  numpy.lib.recfunctions import append_fields

>>> a = numpy.rec.array(a, dtype=[('a', numpy.float64)])
>>> new_a = append_fields(a, 'b', b, usemask=False, dtypes=[numpy.int64])
>>> new_a
array([(1.0, 1), (2.0, 2), (3.0, 3)], 
      dtype=[('a', '<f8'), ('b', '<i8')])

>>> new_a['a']
array([ 1.,  2.,  3.])

>>> new_a['b']
array([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)