我可以在紧凑型记录dtype创建中指定字段名称吗?

时间:2014-06-02 22:46:05

标签: python numpy types

正如Specifying and constructing data types所述,dtype('i4, (5)f8')dtype([('f0', '<i4'), ('f1', '<f8', (5,))])的缩写{<1}}:

  

Numarray引入了一种简写符号,用于将记录格式指定为以逗号分隔的基本格式字符串。

     

此上下文中的基本格式是可选的形状说明符,后跟数组协议类型字符串。如果形状具有多个维度,则需要在该形状上使用括号。 NumPy允许对格式进行修改,因为任何可以唯一标识类型的字符串都可用于指定字段中的数据类型。生成的数据类型字段被命名为'f0','f1',...,'f',其中N(> 1)是字符串中逗号分隔的基本格式的数量。如果提供了可选的形状说明符,则相应字段的数据类型描述子数组。

我的问题是:如果我想明确命名我的字段,是否有任何简写符号?例如,对于dtype([("spam", "f4", (3,)), ("eggs", "f8", (2,2))])

1 个答案:

答案 0 :(得分:0)

http://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.html

使用用户定义的字段名称的几个示例:

Using array-protocol type strings:
>>> np.dtype([('a','f8'),('b','S10')])

...

Using tuples. int is a fixed type, 3 the field’s shape. void is a flexible type, here of size 10:
>>> np.dtype([('hello',(np.int,3)),('world',np.void,10)])

...

Using dictionaries. Two fields named ‘gender’ and ‘age’:
>>> np.dtype({'names':['gender','age'], 'formats':['S1',np.uint8]})

但没有证据表明将名称添加到&#39; i4,(5)f8&#39;输入。下一步是查看我们是否可以找到解析dtype输入的代码。

numpy/core/records.py似乎完成了dtype创作的大部分内容。 numpy/core/_internal.py _commastring解析了&#39;捷径&#39;。

它使用:

format_re = re.compile(asbytes(
                       r'(?P<order1>[<>|=]?)'
                       r'(?P<repeats> *[(]?[ ,0-9L]*[)]? *)'
                       r'(?P<order2>[<>|=]?)'
                       r'(?P<dtype>[A-Za-z0-9.]*(?:\[[a-zA-Z0-9,.]+\])?)'))

解析字符串。它不会寻找names。我的猜测是它是为numarray编写的,numpy前身没有使用字段名。编写自己的解析器并不难。 :)

相关问题