向记录数组追加字段?

0 投票
1 回答
1104 浏览
提问于 2025-04-17 04:09

如果我定义了一个结构化数组:

import numpy as np
x = np.array([(1, 'O', 1)],
             dtype=np.dtype([('step', 'int32'),
                             ('symbol', '|S1'),
                             ('index', 'int32')]))

看起来没问题,直到我这样做:

import numpy.lib.recfunctions as rec
rec.append_fields(x,'x',x['index']+1)

结果是

TypeError: object of type 'numpy.int32' has no len()

这可能是因为 x.shape 的形状是 (1,) 而不是 (1,3)。我该如何给这个结构化数组添加列呢?

1 个回答

1

谢谢大家。在NumPy 1.4.1版本上,我遇到了错误,但我刚刚升级到了git 2.0版本,现在一切都正常了。

撰写回答