python - Can I specify field names in a compact record dtype creation? -
in numpy, dtype('i4, (5)f8')
short dtype([('f0', '<i4'), ('f1', '<f8', (5,))])
, documented @ specifying , constructing data types:
numarray introduced short-hand notation specifying format of record comma-separated string of basic formats.
a basic format in context optional shape specifier followed array-protocol type string. parenthesis required on shape if has more 1 dimension. numpy allows modification on format in string can uniquely identify type can used specify data-type in field. generated data-type fields named 'f0', 'f1', ..., 'f' n (>1) number of comma-separated basic formats in string. if optional shape specifier provided, data-type corresponding field describes sub-array.
my question is: there shorthand notation if want name fields explicitly? example, dtype([("spam", "f4", (3,)), ("eggs", "f8", (2,2))])
?
http://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.html
several examples user defined field names:
using array-protocol type strings: >>> np.dtype([('a','f8'),('b','s10')])
...
using tuples. int fixed type, 3 field’s shape. void flexible type, here of size 10: >>> np.dtype([('hello',(np.int,3)),('world',np.void,10)])
...
using dictionaries. 2 fields named ‘gender’ , ‘age’: >>> np.dtype({'names':['gender','age'], 'formats':['s1',np.uint8]})
but no evidence of syntax adding names 'i4, (5)f8' input. next step see if can find code parses dtype
inputs.
numpy/core/records.py
seems of dtype
creation. numpy/core/_internal.py
_commastring
parses 'short cut'.
it uses:
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,.]+\])?)'))
to parse string. doesn't names
. guess written numarray
, numpy
predecessor did not use fieldnames. shouldn't hard write own parser. :)
Comments
Post a Comment