np.void
I suppose you thought np.void
would work since the type
of a structured array record is void
:
In [32]: node_dtype = np.dtype([("parent", np.void), ("info", np.uint8)])
In [33]: np.zeros(3, node_dtype)
Out[33]:
array([(b'', 0), (b'', 0), (b'', 0)],
dtype=[('parent', 'V'), ('info', 'u1')])
In [34]: type(_[0])
Out[34]: numpy.void
But notice that
In [35]: __['parent']
Out[35]: array([b'', b'', b''], dtype='|V0')
That field occupies 0 bytes.
In [36]: np.zeros(3, np.void)
Out[36]: array([b'', b'', b''], dtype='|V0')
In [37]: np.zeros(3, np.void(0))
Out[37]: array([b'', b'', b''], dtype='|V0')
In [38]: np.zeros(3, np.void(5))
Out[38]:
array([b'\x00\x00\x00\x00\x00', b'\x00\x00\x00\x00\x00',
b'\x00\x00\x00\x00\x00'], dtype='|V5')
In [39]: _[0] = b'12345'
np.void
normally takes an argument, an integer specifying the length.
While it is possible to nest dtypes, the result must still have a known itemsize
:
In [57]: dt0 = np.dtype('i,f')
In [58]: dt1 = np.dtype([('f0','U3'), ('nested',dt0)])
In [59]: dt1
Out[59]: dtype([('f0', '<U3'), ('nested', [('f0', '<i4'), ('f1', '<f4')])])
In [60]: dt1.itemsize
Out[60]: 20
The resulting array will have a know size data buffer, just enough to hold arr.size
items of arr.itemsize
bytes.
object dtype
You can construct a structured array with object
dtype fields
In [61]: arr = np.empty(3, 'O,i')
In [62]: arr
Out[62]:
array([(None, 0), (None, 0), (None, 0)],
dtype=[('f0', 'O'), ('f1', '<i4')])
In [63]: arr[1]['f0']=arr[0]
In [64]: arr[2]['f0']=arr[1]
In [65]: arr
Out[65]:
array([(None, 0), ((None, 0), 0), (((None, 0), 0), 0)],
dtype=[('f0', 'O'), ('f1', '<i4')])
In [66]: arr[0]['f1']=100
In [67]: arr
Out[67]:
array([(None, 100), ((None, 100), 0), (((None, 100), 0), 0)],
dtype=[('f0', 'O'), ('f1', '<i4')])
In [68]: arr[1]['f1']=200
In [69]: arr[2]['f1']=300
In [70]: arr
Out[70]:
array([(None, 100), ((None, 100), 200), (((None, 100), 200), 300)],
dtype=[('f0', 'O'), ('f1', '<i4')])
I don't know if this would a particularly useful structure or not. A list might just as good
In [71]: arr.tolist()
Out[71]: [(None, 100), ((None, 100), 200), (((None, 100), 200), 300)]