3

Here's what happens if I initialize a struct array with the same field names and types in different ways:

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

So initializing with 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 initializing with a dict doesn't, and the problem is the string type:

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

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

Numpy version: 1.8.0

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

1 Answer 1

3

As a workaround, it works if you specify the string width:

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

Probably related to this and this. If it isn't a bug, it is awfully close...

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, forgot to reply to this. Thanks for the suggestion, but unfortunately I don't know the string width in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.