The following code might help:
import numpy as np
dt = np.dtype([('name1', '|S10'), ('name2', '<f8')])
tuplelist=[
('n1', 1.2),
('n2', 3.4),
]
arr = np.array(tuplelist, dtype=dt)
print(arr['name1'])
# ['n1' 'n2']
print(arr['name2'])
# [ 1.2 3.4]
Your immediate problem was that np.dtype expects the format specifiers to be numpy types, such as '|S10' or '<f8' and not Python types, such as str or float. If you type help(np.dtype) you'll see many examples of how np.dtypes can be specified. (I've only mentioned a few.)
Note that np.array expects a list of tuples. It's rather particular about that.
A list of lists raises TypeError: expected a readable buffer object.
A (tuple of tuples) or a (tuple of lists) raises ValueError: setting an array element with a sequence.
W3Schools
w3schools.com › python › numpy › numpy_data_types.asp
NumPy Data Types
Below is a list of all data types in NumPy and the characters used to represent them. i - integer · b - boolean · u - unsigned integer · f - float · c - complex float · m - timedelta · M - datetime · O - object · S - string · U - unicode string · V - fixed chunk of memory for other type ( void ) The NumPy array object has a property called dtype that returns the data type of the array: Get the data type of an array object: import numpy as np arr = np.array([1, 2, 3, 4]) print(arr.dtype) Try it Yourself » ·
NumPy
numpy.org › doc › stable › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.5 Manual
This style does not accept align in the dtype constructor as it is assumed that all of the memory is accounted for by the array interface description. ... Try it in your browser! Data-type with fields big (big-endian 32-bit integer) and little (little-endian 32-bit integer): ... This style has two required and three optional keys. The names and formats keys are required. Their respective values are equal-length lists with the field names and the field formats.
08:42
Learn NumPy data types in 8 minutes! 💱 - YouTube
05:44
NumPy Data Types (dtype) Explained - Complete Guide for Beginners ...
6. DataTypes in NumPy | Complete Python NumPy Tutorial for ...
03:57
Data Type Objects, dtype in numpy - YouTube
16:20
Numpy Tutorial #6 - benannte DTypes (Python für Data Science) ...
NumPy
numpy.org › devdocs › user › basics.types.html
Data types — NumPy v2.6.dev0 Manual
NumPy generally returns elements of arrays as array scalars (a scalar with an associated dtype). Array scalars differ from Python scalars, but for the most part they can be used interchangeably (the primary exception is for versions of Python older than v2.x, where integer array scalars cannot act as indices for lists ...
Programiz
programiz.com › python-programming › numpy › datatypes
NumPy Data Types (With Examples)
Here's the list of most commonly used numeric data types in NumPy: int8, int16, int32, int64 - signed integer types with different bit sizes · uint8, uint16, uint32, uint64 - unsigned integer types with different bit sizes · float32, float64 - floating-point types with different precision ...
NumPy
numpy.org › doc › 2.2 › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.2 Manual
The field names must be strings and the field formats can be any object accepted by dtype constructor. When the optional keys offsets and titles are provided, their values must each be lists of the same length as the names and formats lists.
Top answer 1 of 2
2
Discovered that, despite not having dictionary methods like .items(), you can still call dtype['<column_name>'].
column_names = list(arr.dtype.names)
dtypes = [str(arr.dtype[n]) for n in column_names]
>>> dtypes
['float64', 'float64']
2 of 2
0
Or, as @hpaulj hinted, in one step:
>>> [str(v[0]) for v in arr.dtype.fields.values()]
['float64', 'float64']
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.5 Manual
NumPy generally returns elements of arrays as array scalars (a scalar with an associated dtype). Array scalars differ from Python scalars, but for the most part they can be used interchangeably (the primary exception is for versions of Python older than v2.x, where integer array scalars cannot act as indices for lists ...
Python Course
python-course.eu › numerical-programming › numpy-data-objects-dtype.php
3. Numpy Data Objects, dtype | Numerical Programming
Tuples define the structure of one unit of data; Lists define the dimension or shape of the array. Now, let’s extend our data structure to include more fields: country name, density, area, and population. import numpy as np # Define a structured dtype dt = np.dtype([ ('country', 'S20'), ...
NumPy
numpy.org › doc › 2.3 › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.3 Manual
The field names must be strings and the field formats can be any object accepted by dtype constructor. When the optional keys offsets and titles are provided, their values must each be lists of the same length as the names and formats lists.
TutorialsPoint
tutorialspoint.com › numpy › numpy_data_types.htm
NumPy - Data Types
Original array: ['1.1' '2.2' 'three' '4.4' '5.5'] Original dtype: <U5 Converted array: [1.1 2.2 nan 4.4 5.5] Converted dtype: float64 · You can also convert the data type of existing arrays using the view() method to change the interpretation of the data without changing the underlying bytes. Here, the data is reinterpreted as "float32", resulting in unexpected values because the underlying bytes remain unchanged − · import numpy as np # Creating an array of integers g = np.array([1, 2, 3, 4], dtype=np.int32) print("Original array:", g) print("Original dtype:", g.dtype) # Viewing the array as float32 g_view = g.view(np.float32) print("Viewed array:", g_view) print("Viewed dtype:", g_view.dtype)
NumPy
numpy.org › doc › 2.1 › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.1 Manual
This style does not accept align in the dtype constructor as it is assumed that all of the memory is accounted for by the array interface description. ... This style has two required and three optional keys. The names and formats keys are required. Their respective values are equal-length lists with the field names and the field formats.
NumPy
numpy.org › doc › stable › reference › generated › numpy.dtype.html
numpy.dtype — NumPy v2.5 Manual
Boolean indicating whether the byte order of this dtype is native to the platform. ... The element size of this data-type object. ... A character code (one of ‘biufcmMOSTUV’) identifying the general kind of data. ... Either None or a readonly dictionary of metadata (mappingproxy). ... A bit-width name for this data-type. ... Ordered list ...
NumPy
numpy.org › doc › 1.25 › user › basics.types.html
Data types — NumPy v1.25 Manual
NumPy numerical types are instances of dtype (data-type) objects, each having unique characteristics. Once you have imported NumPy using >>> import numpy as np the dtypes are available as np.bool_, np.float32, etc. Advanced types, not listed above, are explored in section Structured arrays.
NumPy
numpy.org › doc › 2.2 › reference › routines.dtypes.html
Data type classes (numpy.dtypes) — NumPy v2.2 Manual
Previously DType classes were only accessible indirectly. The following are the classes of the corresponding NumPy dtype instances and NumPy scalar types. The classes can be used in isinstance checks and can also be instantiated or used directly. Direct use of these classes is not typical, ...
NumPy
numpy.org › devdocs › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.6.dev0 Manual
This style does not accept align in the dtype constructor as it is assumed that all of the memory is accounted for by the array interface description. ... Try it in your browser! Data-type with fields big (big-endian 32-bit integer) and little (little-endian 32-bit integer): ... This style has two required and three optional keys. The names and formats keys are required. Their respective values are equal-length lists ...
NumPy
numpy.org › devdocs › reference › routines.dtypes.html
Data type classes (numpy.dtypes) — NumPy v2.6.dev0 Manual
Previously DType classes were only accessible indirectly. The following are the classes of the corresponding NumPy dtype instances and NumPy scalar types. The classes can be used in isinstance checks and can also be instantiated or used directly. Direct use of these classes is not typical, ...
APXML
apxml.com › courses › essential-numpy-pandas › chapter-2-getting-started-numpy-arrays › array-data-types
NumPy Array Data Types (dtypes)
Original Array: [1.1 2.7 3.5 4.9] Original dtype: float64 Converted to int32: [1 2 3 4] New dtype: int32 Numeric Array: [ 0 1 5 0 -2] Converted to bool: [False True True False True] New dtype: bool Converted to string: [b'1' b'2' b'3' b'4'] New dtype: |S11 · Caution: Be mindful when using astype(). Converting from a float to an integer truncates the decimal part, it doesn't round. Converting from a higher-precision type (like float64) to a lower-precision one (like float32) can lead to loss of precision. Converting to a type with a smaller range (like int64 to int8) can lead to unexpected results or errors if the values exceed the target type's limits. Understanding NumPy's data types is a fundamental step.
NumPy
numpy.org › doc › 2.3 › reference › routines.dtypes.html
Data type classes (numpy.dtypes) — NumPy v2.3 Manual
Previously DType classes were only accessible indirectly. The following are the classes of the corresponding NumPy dtype instances and NumPy scalar types. The classes can be used in isinstance checks and can also be instantiated or used directly. Direct use of these classes is not typical, ...