w3resource

NumPy Data type: dtype() function

numpy.dtype() function

The dtype() function is used to create a data type object.

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.

Version: 1.15.0

Syntax:

class numpy.dtype(obj, align=False, copy=False)

Parameter:

Name Description Required /
Optional
obj Object to be converted to a data type object. Required
align 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. Optional
copy : bool, 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. optional

Return value:

dtype : dtype or Python type - The data type of rep.

Example: numpy.dtype() function

>>> import numpy as np
>>> np.dtype(np.int16)
dtype('int16')
>>> np.dtype([('f1', np.int16)])
dtype([('f1', '<i2')])
>>> np.dtype([('f1', [('f1', np.int16)])])
dtype([('f1', [('f1', '<i2')])])
>>> np.dtype([('f1', np.uint), ('f2', np.int32)])
dtype([('f1', '<u8'), ('f2', '<i4')])
>>> np.dtype([('a','f8'),('b','S10')])
dtype([('a', '<f8'), ('b', 'S10')])
>>> np.dtype("i4, (2,3)f8")
dtype([('f0', '<i4'), ('f1', '<f8', (2, 3))])
>>> np.dtype([('hello',(int,3)),('world',np.void,10)])
dtype([('hello', '<i8', (3,)), ('world', 'V10')])
>>> np.dtype((np.int16, {'x':(np.int8,0), 'y':(np.int8,1)}))
dtype([('x', 'i1'), ('y', 'i1')])
>>> np.dtype({'names':['gender','age'], 'formats':['S1',np.uint8]})
dtype([('gender', 'S1'), ('age', 'u1')])
>>> np.dtype({'surname':('S25',0),'age':(np.uint8,25)})
dtype([('surname', 'S25'), ('age', 'u1')])

Python - NumPy Code Editor:

Previous: obj2sctype()
Next: format_parser()



Follow us on Facebook and Twitter for latest update.