{ } Raw JSON

bundles / numpy 2.4.4 / numpy / dtype

_DTypeMeta

numpy:dtype

source: /numpy/__init__.py

Summary

Create a data type object.

Extended Summary

A numpy array is homogeneous, and contains elements described by a dtype object. A dtype object can be constructed from different combinations of fundamental numeric types.

Parameters

dtype

Object to be converted to a data type object.

align : bool, optional

Add padding to the fields to match what a C compiler would output for a similar C-struct. Can be True only if obj is a dictionary or a comma-separated string. If a struct dtype is being created, this also sets a sticky alignment flag isalignedstruct.

copy : bool, optional

Make a new copy of the data-type object. If False, the result may just be a reference to a built-in data-type object.

metadata : dict, optional

An optional dictionary with dtype metadata.

Examples

Using array-scalar type:
import numpy as np
np.dtype(np.int16)
Structured type, one field name 'f1', containing int16:
np.dtype([('f1', np.int16)])
Structured type, one field named 'f1', in itself containing a structured type with one field:
np.dtype([('f1', [('f1', np.int16)])])
Structured type, two fields: the first field contains an unsigned int, the second an int32:
np.dtype([('f1', np.uint64), ('f2', np.int32)])
Using array-protocol type strings:
np.dtype([('a','f8'),('b','S10')])
Using comma-separated field formats. The shape is (2,3):
np.dtype("i4, (2,3)f8")
Using tuples. ``int`` is a fixed type, 3 the field's shape. ``void`` is a flexible type, here of size 10:
np.dtype([('hello',(np.int64,3)),('world',np.void,10)])
Subdivide ``int16`` into 2 ``int8``'s, called x and y. 0 and 1 are the offsets in bytes:
np.dtype((np.int16, {'x':(np.int8,0), 'y':(np.int8,1)}))
Using dictionaries. Two fields named 'gender' and 'age':
np.dtype({'names':['gender','age'], 'formats':['S1',np.uint8]})
Offsets in bytes, here 0 and 25:
np.dtype({'surname':('S25',0),'age':(np.uint8,25)})

See also

result_type

Aliases

  • numpy.dtype

Referenced by