Numpy structured arrays: string type not understood when specifying dtype with dict

Here's what happens if I initialize an array of a struct with the same field names and types differently:

>>> a = np.zeros(2, dtype=[('x','int64'),('y','a')])
>>> a
array([(0L, ''), (0L, '')],
 dtype=[('x', '<i8'), ('y', 'S')])

      

So initialization with a list of tuples works fine.

>>> mdtype = dict(names=['x','y'],formats=['int64','a'])
>>> mdtype
{'names': ['x', 'y'], 'formats': ['int64', 'a']}
>>> a = np.zeros(2,dtype=mdtype)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: data type not understood

      

So initialization with a dict doesn't, and the problem is with the string type:

>>> mdtype = dict(names=['x','y'],formats=['int64','float64'])
>>> a = np.zeros(2,dtype=mdtype)
>>>

      

No problems. Any ideas? Is this a Numpy bug?

CD version: 1.8.0

Python 2.7.6 (default, Nov 10, 2013 7:24:24 PM) [MSC v.1500 64 bit (AMD64)] on win32

+3


source to share


1 answer


As a workaround, it works by specifying the line width:

>>> mdtype = dict(names=['x','y'],formats=['int64','a1'])
>>> np.dtype(mdtype)
dtype([('x', '<i8'), ('y', 'S1')])

      



Perhaps this is also related to this . If it's not a mistake, it's awfully close ...

+3


source







All Articles