2

I want to declare a numpy array (arr) with elements having a certain format, like this:

dt1 = np.dtype([('sec', '<i8'), ('nsec', '<i8')])
dt2 = np.dtype([('name', 'S10'), ('value', '<i4')])

arr = np.array([('x', dtype=dt1), ('y', dtype=dt2)])

The structure is something like this:

arr['x'] = elements with the format dt1 (sec and nsec)
arr['y'] = array of n elements with the format dt2 (name and value)

And the elements within should be accessed like this:

arr['x']['sec'], arr['x']['nsec'], arr['y'][0]['name'] etc.

But I get an invalid syntax error. What is the correct syntax in this situation?

3
  • Can you explain in words what structure you want your array to have? It's not obvious from your broken syntax exactly what you were trying to do. What shape do you want your array to have? What do you want its dtype to be? How would you access its elements? Commented Aug 25, 2016 at 20:09
  • I want the structure to be something like this: arr['x'] = elements with the format dt1 (sec and nsec) arr['y'] = array of n elements with the format dt2 (name and value) And the elements within could be accessed like this: arr['x']['sec'], arr['x']['nsec'], arr['y'][0]['name'] etc. Is it possible to represent it like this? Or is there a workaround this? Commented Aug 25, 2016 at 20:20
  • Put that description in your question, with clear formatting. Commented Aug 25, 2016 at 20:23

1 Answer 1

3

A doubly compound dtype might work:

In [834]: dt1 = np.dtype([('sec', '<i8'), ('nsec', '<i8')])
     ...: dt2 = np.dtype([('name', 'S10'), ('value', '<i4')])
     ...: 
In [835]: dt = np.dtype([('x', dt1),('y', dt2)])

In [837]: z=np.ones((3,), dtype=dt)
In [838]: z
Out[838]: 
array([((1, 1), (b'1', 1)), ((1, 1), (b'1', 1)), ((1, 1), (b'1', 1))], 
      dtype=[('x', [('sec', '<i8'), ('nsec', '<i8')]), ('y', [('name', 'S10'), ('value', '<i4')])])

In [839]: z['x']['sec']
Out[839]: array([1, 1, 1], dtype=int64)

In [841]: z['y'][0]['name']='y0 name'
In [842]: z
Out[842]: 
array([((1, 1), (b'y0 name', 1)), ((1, 1), (b'1', 1)), ((1, 1), (b'1', 1))], 
      dtype=[('x', [('sec', '<i8'), ('nsec', '<i8')]), ('y', [('name', 'S10'), ('value', '<i4')])])
In [843]: z[0]
Out[843]: ((1, 1), (b'y0 name', 1))

A dictionary would work with similar syntax

In [845]: d={'x':np.ones((3,),dt1), 'y':np.zeros((4,),dt2)}
In [846]: d
Out[846]: 
{'x': array([(1, 1), (1, 1), (1, 1)], 
       dtype=[('sec', '<i8'), ('nsec', '<i8')]),
 'y': array([(b'', 0), (b'', 0), (b'', 0), (b'', 0)], 
       dtype=[('name', 'S10'), ('value', '<i4')])}
In [847]: d['y'][0]['name']='y0 name'
Sign up to request clarification or add additional context in comments.

1 Comment

how could I declare the numpy array of an unknown length?

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.