  # Object data type (dtype) in NumPy Python

Arrays | NumPy | Python Methods and Functions

Each ndarray has an associated data type object (dtype). This data type object (dtype) informs us about the layout of the array. This means it gives us information about:

• Data type (integer, floating point, Python object, etc.)
• Data size (number of bytes )
• Data byte order (big or big endian)
• If the data type is a subarray, what is its shape and data type.

The ndarray values ​​are stored in a buffer, which can be thought of as a contiguous block of bytes of memory. How these bytes are interpreted is determined by the dtype object.

1. Data type object (dtype) construction. A datatype object is an instance of the numpy.dtype class and can be instantiated using numpy.dtype.

Parameters:

• obj: an object to convert to a datatype object.
• align : bool, optional
Padding the margins to match what the C compiler would output for a similar C structures.
• copy : bool, optional
Make a fresh copy of the datatype object. If False, the result can simply be a reference to an embedded data type object.

 ` # Python program to create data type object ` ` import ` ` numpy as np `   ` # np.int16 is converted to data type object. ` ` print ` ` (np.dtype (np.int16)) `

Exit :

`int16`

` `

` # Python program to create a data type object # contains a 32-bit big endian integer import numpy as np   # i4 represents integer a 4 byte number # & gt; represents big endian byte order and & lt; represents big endian encoding. # dt is a dtype object dt = np.dtype ( `& gt; i4` )    print ( "Byte order is:" , dt.byteorder)   print ( "Size is:" , dt.itemsize)   print ( "Data type is:" , dt.name) `

` `

Output:

` Byte order is: & gt; Size is: 4 Name of data type is: int32 `

The type specifier (i4 in the above case) can take various forms:

1. b1, i1, i2, i4, i8 , u1, u2, u4, u8, f2, f4, f8, c8, c16, a
(representing bytes, integers, unsigned integers, floating point numbers, complex and
fixed length strings of specified length in bytes )
2. int8,…, uint8,…, float16, float32, float64, complex64, complex128
(this time with bitwise dimensions)

Notes:

` dtype is different from type. `

` `

` # Python program to differentiate # between type and dtype. import numpy as np   a = np.array ([ 1 ])    print ( "type is:" , type (a)) print ( "dtype is:" , a.dtype) `

Exit:

` type is: dtype is: int32 `
2. Data type objects with structured arrays. Data type objects are useful for creating structured arrays. Structured array — this is the one that contains different types of data. Structured arrays can be accessed using fields.
A field is like giving a name to an object. In the case of structured arrays, the dtype will also be structured.

 ` # Python demo program ` ` # using fields ` ` import ` ` numpy as np `   ` # A structured data type containing a 16-character string (in the" name "field) ` ` # and a subarray of two 64-bit floating point numbers (in the & # 39; grades & # 39; field): ` `  `` `` dt = np.dtype ([( `name` , np.unicode_, 16 ), ( `grades` , np.float64, (` ` 2 ` `,))]) `   ` # Object data type with field ratings ` ` print ` ` (dt [` ` `grades` ` `]) `   ` # Object data type with field name ` ` print ` ` (dt [` ` `name` ` `]) `

Exit:

` (`& lt; f8`, (2,)) `

` `

` # Python program for demonstration # using a structured array data type object. < code class = "keyword"> import numpy as np   dt = np.dtype ([( `name` , np.unicode_, 16 ), ( `grades` , np.float64, ( 2 ,))])   # x is a structured array with the names and marks of students. # Data type of student name np.unicode_ and # mark data type - np.float (64) x = np.array ([( `Sarah` , ( 8.0 , 7.0 )), ( ` John` , ( 6.0 , 7.0 ))], dtype = dt)   print (x [ 1 ]) print ( " Grades of John are: " , x [ 1 ] [ `grades` ]) print ( " Names are: " , x [ ` name` ] ) `

` `

Output:

` (`John`, [6., 7.]) Grades of John are: [6. 7.] Names are: [` Sarah` `John`] `