You're making a small mistake here. You're assuming that dtype.name and dtype.names return the same thing. They do not.

From the docs

name A bit-width name for this data-type.

names Ordered list of field names, or None if there are no fields.


So what you're seeing is the bit-width name for the data type of your field. However, if you called dtype.names, you would be returned None, as the single field you have passed does not have any fields to return.


As far as I know, there is not a way to infer the name of a field without access to the structured array that contains it. You will most likely have to pass the field name as a parameter to your empty_check function.

Answer from user3483203 on Stack Overflow
🌐
w3resource
w3resource.com › python-exercises › numpy › access-and-print-name-field-from-numpy-structured-array.php
Access and print Name Field from NumPy Structured array
September 4, 2025 - Access the 'name' field from the structured array using structured_array['name']. Print all values of the 'name' field. ... Write a Numpy program to extract and print the 'name' field from a structured array and then sort the names alphabetically.
🌐
NumPy
numpy.org › doc › stable › user › basics.rec.html
Structured arrays — NumPy v2.5 Manual
A convenience function numpy.lib.recfunctions.repack_fields converts an aligned dtype or array to a packed one and vice versa. It takes either a dtype or structured ndarray as an argument, and returns a copy with fields re-packed, with or without padding bytes. In addition to field names, fields may also have an associated title, an alternate name, which is sometimes used as an additional description or alias for the field.
🌐
Medium
medium.com › @mitchparker99 › numpy-structured-arrays-43a08f4de81a
Numpy: Structured Arrays. Structured arrays are ndarrays whose… | by Mitchell Parker | Medium
June 19, 2024 - get_names_flat: Returns the field names of the input datatype as a tuple, flattened. from numpy.lib import recfunctions as rfn adtype = np.dtype([('a', int), ('b', [('ba', int), ('bb', int)])]) rfn.get_names_flat(adtype) These are just a few examples, and there are many more functions available in numpy.lib.recfunctions for working with structured arrays.
🌐
SciPy
docs.scipy.org › doc › numpy-1.7.0 › user › basics.rec.html
Structured arrays (aka “Record arrays”) — NumPy v1.7 Manual (DRAFT)
Here we have created a one-dimensional ... position we get the second record: ... Conveniently, one can access any field of the array by indexing using the string that names that field....
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-numpy-how-to-get-colum-names-of-ndarray
Python NumPy: How to Get Column Names from a Structured Array (and Plain Arrays) | Tutorial Reference
The methods for getting "column ... with names=True or by defining a structured dtype), the names of these fields (columns) are accessible via the dtype.names attribute....
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.1 › user › basics.rec.html
Structured arrays — NumPy v1.10 Manual
In this case, an array is produced that looks and acts like a simple int32 array, but also has definitions for fields that use only one byte of the int32 (a bit like Fortran equivalencing). 3) List argument: In this case the record structure is defined with a list of tuples. Each tuple has 2 or 3 elements specifying: 1) The name of the field (‘’ is permitted), 2) the type of the field, and 3) the shape (optional).
🌐
NumPy
numpy.org › devdocs › user › basics.rec.html
Structured arrays — NumPy v2.6.dev0 Manual
A convenience function numpy.lib.recfunctions.repack_fields converts an aligned dtype or array to a packed one and vice versa. It takes either a dtype or structured ndarray as an argument, and returns a copy with fields re-packed, with or without padding bytes. In addition to field names, fields may also have an associated title, an alternate name, which is sometimes used as an additional description or alias for the field.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_field_access.htm
NumPy - Field access
Accessing with a List of Field Names: You can specify multiple field names in a list to get a structured array containing only those fields. Using Field Indexing with Structured Arrays: If you need to access fields by their indices, you can ...
Find elsewhere
Top answer
1 of 2
2

I don't know if displaying the resulting array helps you visualize the nesting or not.

In [279]: c = [('x','f8'),('y','f8')]
     ...: A = [('data_string','|S20'),('data_val', c, 2)]
     ...: arr = np.zeros(2, dtype=A)
In [280]: arr
Out[280]: 
array([(b'', [(0., 0.), (0., 0.)]), (b'', [(0., 0.), (0., 0.)])],
      dtype=[('data_string', 'S20'), ('data_val', [('x', '<f8'), ('y', '<f8')], (2,))])

Note how the nesting of () and [] reflects the nesting of the fields.

arr.dtype only has direct access to the top level field names:

In [281]: arr.dtype.names
Out[281]: ('data_string', 'data_val')
In [282]: arr['data_val']
Out[282]: 
array([[(0., 0.), (0., 0.)],
       [(0., 0.), (0., 0.)]], dtype=[('x', '<f8'), ('y', '<f8')])

But having accessed one field, we can then look at its fields:

In [283]: arr['data_val'].dtype.names
Out[283]: ('x', 'y')
In [284]: arr['data_val']['x']
Out[284]: 
array([[0., 0.],
       [0., 0.]])

Record number indexing is separate, and can be multidimensional in the usual sense:

In [285]: arr[1]['data_val']['x'] = [1,2]
In [286]: arr[0]['data_val']['y'] = [3,4]
In [287]: arr
Out[287]: 
array([(b'', [(0., 3.), (0., 4.)]), (b'', [(1., 0.), (2., 0.)])],
      dtype=[('data_string', 'S20'), ('data_val', [('x', '<f8'), ('y', '<f8')], (2,))])

Since the data_val field has a (2,) shape, we can mix/match that index with the (2,) shape of arr:

In [289]: arr['data_val']['x']
Out[289]: 
array([[0., 0.],
       [1., 2.]])
In [290]: arr['data_val']['x'][[0,1],[0,1]]
Out[290]: array([0., 2.])
In [291]: arr['data_val'][[0,1],[0,1]]
Out[291]: array([(0., 3.), (2., 0.)], dtype=[('x', '<f8'), ('y', '<f8')])

I mentioned that fields indexing is like dict indexing. Note this display of the fields:

In [294]: arr.dtype.fields
Out[294]: 
mappingproxy({'data_string': (dtype('S20'), 0),
              'data_val': (dtype(([('x', '<f8'), ('y', '<f8')], (2,))), 20)})

Each record is stored as a block of 52 bytes:

In [299]: arr.itemsize
Out[299]: 52
In [300]: arr.dtype.str
Out[300]: '|V52'

20 of those are data_string, and 32 are the 2 c fields

In [303]: arr['data_val'].dtype.str
Out[303]: '|V16'

You can ask for a list of fields, and get a special kind of view. Its dtype display is a little different

In [306]: arr[['data_val']]
Out[306]: 
array([([(0., 3.), (0., 4.)],), ([(1., 0.), (2., 0.)],)],
      dtype={'names': ['data_val'], 'formats': [([('x', '<f8'), ('y', '<f8')], (2,))], 'offsets': [20], 'itemsize': 52})

In [311]: arr['data_val'][['y']]
Out[311]: 
array([[(3.,), (4.,)],
       [(0.,), (0.,)]],
      dtype={'names': ['y'], 'formats': ['<f8'], 'offsets': [8], 'itemsize': 16})

Each 'data_val' starts 20 bytes into the 52 byte record. And each 'y' starts 8 bytes into its 16 byte record.

2 of 2
1

The statement zeros['data_val'] creates a view into the array, which may already be non-contiguous at that point. You can extract multiple values of x because c is an array type, meaning that x has clearly defined strides and shape. The semantics of the statement zeros[:, 'x'] are very unclear. For example, what happens to data_string, which has no x? I would expect an error; you might expect something else.

The only way I can see the index being simplified, is if you expand c into A directly, sort of like an anonymous structure in C, except you can't do that easily with an array.

🌐
Scaler
scaler.com › home › topics › numpy › indexing structured arrays in numpy
Indexing Structured Arrays in NumPy - Scaler Topics
November 9, 2022 - We can assign an index to a multi-field structured array in NumPy, where the indexes are a list of field names, respectively.
🌐
NumPy
numpy.org › doc › 1.13 › user › basics.rec.html
Structured arrays — NumPy v1.13 Manual
Here we have created a one-dimensional ... or less. If we index this array at the second position we get the second structure: ... Conveniently, one can access any field of the array by indexing using the string that names that field....
Top answer
1 of 1
4

You access the fields of a structured array by field name. There isn't a way around this. Unless the dtypes let you view it in a different way.

Lets call your desire output c.

In [1061]: b['fd']
Out[1061]: 
array([[  1.10000000e+01,   2.10000000e+01,   3.10000000e+01,
          1.00000000e-02],
       [  4.10000000e+01,   5.10000000e+01,   6.10000000e+01,
          1.10000000e-01],
       [  7.10000000e+01,   8.10000000e+01,   9.10000000e+01,
          2.10000000e-01]])

What I think you are trying to do is collect these values for both fields:

In [1062]: b['fd'][:,0]
Out[1062]: array([ 11.,  41.,  71.])

In [1064]: c['fd']
Out[1064]: 
array([[ 11.],
       [ 41.],
       [ 71.]])

As I just explained in https://stackoverflow.com/a/38090370/901925 the recfunctions generally allocate a target array and copy values by field.

So the field iteration solution would be something like:

In [1066]: c.dtype
Out[1066]: dtype([('fd', '<f8', (1,)), ('av', '<f8', (1,))])

In [1067]: b.dtype
Out[1067]: dtype([('fd', '<f8', (4,)), ('av', '<f8', (4,))])

In [1068]: d=np.zeros((b.shape), dtype=c.dtype)


In [1070]: for n in b.dtype.names:
    d[n][:] = b[n][:,[0]]

In [1071]: d
Out[1071]: 
array([([11.0], [1.0]), ([41.0], [4.0]), ([71.0], [7.0])], 
      dtype=[('fd', '<f8', (1,)), ('av', '<f8', (1,))])

================

Since both fields a floats, I can view b as a 2d array; and select the 2 subcolumns with 2d array indexing:

In [1083]: b.view((float,8)).shape
Out[1083]: (3, 8)

In [1084]: b.view((float,8))[:,[0,4]]
Out[1084]: 
array([[ 11.,   1.],
       [ 41.,   4.],
       [ 71.,   7.]])

Similarly, c can be viewed as 2d

In [1085]: c.view((float,2))
Out[1085]: 
array([[ 11.,   1.],
       [ 41.,   4.],
       [ 71.,   7.]])

And I can, then port the values to a blank d with:

In [1090]: d=np.zeros((b.shape), dtype=c.dtype)

In [1091]: d.view((float,2))[:]=b.view((float,8))[:,[0,4]]

In [1092]: d
Out[1092]: 
array([([11.0], [1.0]), ([41.0], [4.0]), ([71.0], [7.0])], 
      dtype=[('fd', '<f8', (1,)), ('av', '<f8', (1,))])

So, at least in this case, we don't have to do field by field copy. But I can't say, without testing, which is faster. In my previous answer I found that field by field copy was relatively fast when dealing with many rows.

🌐
NumPy
numpy.org › doc › 1.22 › user › basics.rec.html
Structured arrays — NumPy v1.22 Manual
A convenience function numpy.lib.recfunctions.repack_fields converts an aligned dtype or array to a packed one and vice versa. It takes either a dtype or structured ndarray as an argument, and returns a copy with fields re-packed, with or without padding bytes. In addition to field names, fields may also have an associated title, an alternate name, which is sometimes used as an additional description or alias for the field.
🌐
University of Texas at Austin
het.as.utexas.edu › HET › Software › Numpy › user › basics.rec.html
Structured arrays (aka “Record arrays”) — NumPy v1.9 Manual
Here we have created a one-dimensional ... position we get the second record: ... Conveniently, one can access any field of the array by indexing using the string that names that field....
🌐
Omz Software
omz-software.com › pythonista › numpy › user › basics.rec.html
Structured arrays (aka “Record arrays”) — NumPy v1.8 Manual
Here we have created a one-dimensional ... position we get the second record: ... Conveniently, one can access any field of the array by indexing using the string that names that field....
🌐
NumPy
numpy.org › doc › stable › user › basics.rec.html
Structured arrays — NumPy v2.4 Manual
A convenience function numpy.lib.recfunctions.repack_fields converts an aligned dtype or array to a packed one and vice versa. It takes either a dtype or structured ndarray as an argument, and returns a copy with fields re-packed, with or without padding bytes. In addition to field names, fields may also have an associated title, an alternate name, which is sometimes used as an additional description or alias for the field.
🌐
LabEx
labex.io › tutorials › python-structured-arrays-in-numpy-85704
Structured Arrays in NumPy | Programming Tutorials | LabEx
## Convert a record array to a structured array x = recordarr.view(dtype=[('name', 'U10'), ('age', int)]) In this lab, we learned how to create and work with structured arrays in NumPy. Structured arrays are useful for working with structured data, and they allow us to access and modify individual fields of the array.