I have a complex nested structured array (often used as a recarray). Its simplified for this example, but in the real case there are multiple levels.
c = [('x','f8'),('y','f8')]
A = [('data_string','|S20'),('data_val', c, 2)]
zeros = np.zeros(1, dtype=A)
print(zeros["data_val"]["x"])
I am trying to index the "x" datatype of the nested arrays datatype without defining the preceding named fields. I was hoping something like print(zeros[:,"x"]) would let me slice all of the top level data, but it doesn't work.
Are there ways to do fancy indexing with nested structured arrays with accessing their field names?
I don't know if displaying the resulting array helps you visualize the nesting or not.
Note how the nesting of
()and[]reflects the nesting of the fields.arr.dtypeonly has direct access to the top level field names:But having accessed one field, we can then look at its fields:
Record number indexing is separate, and can be multidimensional in the usual sense:
Since the
data_valfield has a (2,) shape, we can mix/match that index with the (2,) shape ofarr:I mentioned that fields indexing is like
dictindexing. Note this display of the fields:Each record is stored as a block of 52 bytes:
20 of those are
data_string, and 32 are the 2cfieldsYou can ask for a list of fields, and get a special kind of
view. Its dtype display is a little differentEach 'data_val' starts 20 bytes into the 52 byte record. And each 'y' starts 8 bytes into its 16 byte record.