将字段追加到大小为ONE的结构化数组时出现TypeError [英] TypeError when appending fields to a structured array of size ONE

查看:116
本文介绍了将字段追加到大小为ONE的结构化数组时出现TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将字段追加到大小为ONE的结构化数组时遇到运行时错误.我在下面写了一个简单的例子:

I'm getting a run-time error when trying to append field(s) to a structured array of size ONE. I've written a simple example below:

import numpy as np
import numpy.lib.recfunctions as rcfuncs

dtype_ = np.dtype( { 'names': ["field_a","field_b","field_c"]
                  , 'formats': ['S32', 'i4', 'f8']}
                  )
data_ = [("1",17, 123.45)]
numpy_array = np.array(data_, dtype_)            

# append 2 fields
numpy_array = rcfuncs.append_fields( numpy_array,["field_d","field_e"],data=[ "1","3" ] )

# append 1 field fails :(
numpy_array = rcfuncs.append_fields( numpy_array, "field_f", data=["123456"] )

我遇到了错误:

TypeError: descriptor 'ravel' requires a 'numpy.ndarray' object but received a 'numpy.void'

同样,如果我反转"附加内容,则包含两个字段的语句将失败:

As well, if I 'invert' the appends, the statement with the two fields append will fail:

# append 1 field
numpy_array = rcfuncs.append_fields( numpy_array, "field_f", data=["123456"] )

# append 2 fields fails :(
numpy_array = rcfuncs.append_fields( numpy_array,["field_d","field_e"],data=[ "1", "3" ] )

我正在使用python 2.7.11和numpy 1.11.0运行,并且初始数组的大小大于2时我没有问题.

I am running with python 2.7.11 and numpy 1.11.0 and I do not have the issue when the initial array is of size greater than 2.

如何解决类型错误?

谢谢

推荐答案

将可选参数usemask设置为False时,我们没有收到TypeError错误.

We do not get the TypeError when setting the optional parameter usemask to False

numpy_array = \
  rcfuncs.append_fields(numpy_array, "field_f", data=["123456"], usemask=False)
numpy_array = \
  rcfuncs.append_fields(numpy_array,["field_d","field_e"],data=[ "1", "3" ], usemask=False)

这篇关于将字段追加到大小为ONE的结构化数组时出现TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆