Python 当数组名为unicode时,numpy recfunctions追加\u字段是否失败?

Python 当数组名为unicode时,numpy recfunctions追加\u字段是否失败?,python,numpy,unicode,Python,Numpy,Unicode,我正在尝试使用numpy.lib.recfunctions append_fields函数将数组附加到numpy重新排列 如果重新排列字段名是unicode,我会收到一个“TypeError:data type not Understanding”错误 这种行为是否符合设计,如果是,是否有解决办法 在Win32计算机上使用python 2.7 下面的代码演示了问题(请原谅额外的日期-时间操作-我希望创建的问题尽可能接近我的原始错误): 使用unicode名称的代码在Python3下运行良好,其中

我正在尝试使用numpy.lib.recfunctions append_fields函数将数组附加到numpy重新排列

如果重新排列字段名是unicode,我会收到一个“TypeError:data type not Understanding”错误

这种行为是否符合设计,如果是,是否有解决办法

在Win32计算机上使用python 2.7

下面的代码演示了问题(请原谅额外的日期-时间操作-我希望创建的问题尽可能接近我的原始错误):


使用
unicode
名称的代码在
Python3
下运行良好,其中unicode是默认的字符串类型


编辑:起初我认为问题在于屏蔽数组。但是通过进一步的测试,我得出结论,真正的问题是
dtype
是否可以接受unicode(在py2情况下)名称。
通常名称必须是字符串(无论版本如何定义)。但是定义数据类型的字典样式允许使用unicode名称。这就是为什么
fromarrays
有效,而不是
append\u字段
有效的根本原因

我将保留蒙面数组讨论,因为这是您接受的

屏蔽阵列有问题吗? 在2.7中,完整的错误堆栈是:

  File "stack28586238.py", line 22, in <module>
    FileData = RF.append_fields(FileData,'DateTime', data=DT)  #, dtypes='f8'
  File "/usr/local/lib/python2.7/site-packages/numpy/lib/recfunctions.py", line 633, in append_fields
    base = merge_arrays(base, usemask=usemask, fill_value=fill_value)
  File "/usr/local/lib/python2.7/site-packages/numpy/lib/recfunctions.py", line 403, in merge_arrays
    return seqarrays.view(dtype=seqdtype, type=seqtype)
  File "/usr/local/lib/python2.7/site-packages/numpy/core/records.py", line 501, in view
    return ndarray.view(self, dtype, type)
  File "/usr/local/lib/python2.7/site-packages/numpy/ma/core.py", line 2782, in __array_finalize__
    _mask = getattr(obj, '_mask', make_mask_none(obj.shape, odtype))
  File "/usr/local/lib/python2.7/site-packages/numpy/ma/core.py", line 1566, in make_mask_none
    result = np.zeros(newshape, dtype=make_mask_descr(dtype))
  File "/usr/local/lib/python2.7/site-packages/numpy/ma/core.py", line 1242, in make_mask_descr
    return np.dtype(_recursive_make_descr(ndtype, np.bool))
TypeError: data type not understood
在前一个SO问题(不久前)中,我遇到了掩蔽数组的问题。我得看看是否有关联


与此没有直接关系,但它确实指出了混合掩蔽数组和结构化数组时的一些错误

是您先前关于
add_字段
的SO问题,重点是
datetime
类型

或者使用unicode名称定义数据类型时出现问题? 通过添加
usemack=False
我将错误转移到另一个点,在这里它试图从两个组件的数据类型列表中构造
dtype
np.dtype(base.dtype.descr+data.dtype.descr)

在2.7中,我们可以使用unicode名称构造
记录数组

In [11]: np.core.records.fromarrays([[0,1]],names=[u'test'])
Out[11]: 
rec.array([(0,), (1,)], 
      dtype=[(u'test', '<i4')])
似乎通常情况下,数据类型名称必须是字符串。在
python3
中,
np.dtype([(b'test',int)])
产生相同的错误

同样在py3中

np.core.records.fromarrays([[0,1]],names=[b'test'])
生成一个
ValueError:字段名必须是字符串

np.core.records.fromArray
允许使用unicode,因为它使用
格式\u解析器

p=np.format_parser(['int'],[u'test'],[])
p.dtype
# dtype([(u'test', '<i4')])
下面是一个合并两个具有unicode名称的结构化数组的示例(使用py2):

[53]中的
dt1=np.dtype({'names':[u'test1'],'formats':['int']})
在[54]中:dt2=np.dtype({'names':[u'test2'],'formats':['float']})
在[55]中:dt12=np.dtype({'names':[u'test1',u'test2'],'formats':['int','float']})
在[56]中:arr1=np.array([1,2,3]中x的[(x,]),dtype=dt1)
[57]中:arr2=np.array([1,2,3]中x的[(x,)],dtype=dt2)
[58]中:arr12=np.zeros((3,),dtype=dt12)
In[59]:RF.递归填充字段(arr1,arr12)
...
In[60]:RF.递归填充字段(arr2,arr12)
出[60]:
数组([(1,1.0),(2,2.0),(3,3.0)],

dtype=[(u'test1',”进一步挖掘,我发现py2和py3都希望字段名是字符串(由版本定义)。通过在py2中允许unicode名称,异常情况是
fromarrays()
函数。
In [12]: np.dtype([(u'test', int)])
...    
TypeError: data type not understood
np.core.records.fromarrays([[0,1]],names=[b'test'])
p=np.format_parser(['int'],[u'test'],[])
p.dtype
# dtype([(u'test', '<i4')])
np.dtype({'names':[u'test'],'formats':['int']})
In [53]: dt1 = np.dtype({'names':[u'test1'],'formats':['int']})
In [54]: dt2 = np.dtype({'names':[u'test2'],'formats':['float']})
In [55]: dt12 = np.dtype({'names':[u'test1',u'test2'],'formats':['int','float']})

In [56]: arr1 = np.array([(x,) for x in [1,2,3]],dtype=dt1)
In [57]: arr2 = np.array([(x,) for x in [1,2,3]],dtype=dt2)
In [58]: arr12 = np.zeros((3,),dtype=dt12)

In [59]: RF.recursive_fill_fields(arr1,arr12)
...

In [60]: RF.recursive_fill_fields(arr2,arr12)
Out[60]: 
array([(1, 1.0), (2, 2.0), (3, 3.0)], 
      dtype=[(u'test1', '<i4'), (u'test2', '<f8')])