# Python NumPy Data Types

In this tutorial, we will cover datatypes in the NumPy library of Python.

In Numpy, all the items of an array are data type objects that are also known as NumPy dtypes. The data type object is used to implement the fixed size of memory corresponding to an array.

It mainly provides us information about the following:

• It gives us information about the type of data (that is `integer``float` or Python `object`)
• It gives us information about the size of data
• It tells us about the Byte order (little-endian or big-endian)
• In the case of structured type, it tells us about the names of fields, the data type of each field, and part of the memory block taken by each field.
• In the case, if the data type is a subarray it tells us about its shape and data type

In this, the byte order is decided just by prefixing ‘<‘ or ‘>’ to the data type. where the symbol ‘<‘ means that encoding is little-endian (that is least significant is stored in the smallest address) and the symbol ‘>’ means that encoding is big-endian (a most significant byte is stored in the smallest address).

## NumPy `dtype` Object

Given below is a required syntax used to create the `dtype` object:

``numpy.dtype(object, align, copy)  ``

Following is a description of arguments of the above mentioned constructor:

• object: This argument is used to represent the object which is to be converted to the data type.
• align: It is an optional argument as is used to add padding to the fields to match what a C compiler would output for a similar C-struct. This argument can be set to any boolean value
• copy: This argument is used to create a copy of `dtype` object and it is also an optional argument.

The NumPy library mainly provides a higher range of numeric data types than that provided by Python. The list of numeric data types is given in the following table:

Given below is a list of characters that are used to represent `dtype` in Numpy:

• `i` – integer
• `b` – boolean
• `u` – unsigned integer
• `f` – float
• `c` – complex float
• `m` – timedelta
• `M` – datetime
• `O` – object
• `S` – string
• `U` – Unicode string
• `V` – the fixed chunk of memory for other types ( void )

Now its time to dive into some examples.

## Example 1:

Now in the example given below, we will try to find out the data type of the array containing strings:

``````import numpy as np

ar1 = np.array(['chair', 'book', 'notebook'])
print(ar1.dtype)``````

<U8

## Example 2:

In the example given below, we will construct a datatype object. It is important to note here that the data type object is mainly an instance of `numpy.dtype` class and it can also be created using `numpy.dtype` function. Let us see:

``````import numpy as np

dt1 = np.dtype(np.int64)
print (dt1)``````

int64

## Example 3:

Instead of using the int8, int16, int32, int64, etc. usually the simpler represenation using strings ‘i1’, ‘i2’, ‘i3’, and ‘i4’ and so on is preferred. Now we will see an example related to this:

``````import numpy as np

a = np.dtype('i4')
print (a)``````

int32

## Example 4:

In our following example, we will create a structured datatype and then apply it to a `ndarray` object:

``````import numpy as np

# info with ket and value
a = np.dtype([('rollno',np.int16)])
print(a)

a = np.array([(101,),(201,),(301,)], dtype = a)
print (a)``````

[(‘rollno’, ‘<i2’)] [(101,) (201,) (301,)]

## Example 5 – Change datatype:

In the example given below, we will change the datatype from `float` to `integer` by using `int` as a parameter value:

``````import numpy as np

ar= np.array([1.1, 2.1, 3.1])

newarr = ar.astype(int)

print(newarr)
print(newarr.dtype)``````

[1 2 3]

int32

## Summary

In this tutorial, we covered the concept of datatypes in Array. We saw how `dtype` object is used to specify the datatype of values, its syntax, and parameters required for the `dtype` object. We also covered various numeric data types and then a few examples for your understanding.