Python NumPy Data Types

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 integerfloat 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:

SNData typeDescription
1bool_This is used to represents the boolean value indicating true or false. It is stored as a byte.
2int_This is the default type of an integer. It is identical to long type in C that mainly contains 64 bit or 32-bit integer.
3intcThis is similar to the C integer (c int) as it represents 32 or 64-bit int.
4intpThis is used to represent the integers that are used for indexing.
5int8This is the 8-bit integer identical to a byte. The range of the value is -128 to 127.
6int16This is the 2-byte (16-bit) integer and the range is -32768 to 32767.
7int32This is the 4-byte (32-bit) integer. The range is -2147483648 to 2147483647.
8int64This is the 8-byte (64-bit) integer and The range is -9223372036854775808 to 9223372036854775807.
9uint8This is 1-byte (8-bit) unsigned integer.
10uint16This is 2-byte (16-bit) unsigned integer.
11uint32This is 4-byte (32-bit) unsigned integer.
12uint64This is 8 bytes (64-bit) unsigned integer.
13float_This is identical to float64.
14float16This is used to represent the half-precision float. 5 bits are reserved for the exponent. 10 bits are reserved for the mantissa, and 1 bit is reserved for the sign.
15float32This is used to represent single-precision float. 8 bits are reserved for the exponent, 23 bits are reserved for the mantissa, and 1 bit is reserved for the sign.
16float64This is used to represent double-precision float. 11 bits are reserved for the exponent, 52 bits are reserved for the mantissa, 1 bit is used for the sign.
17complex_This is identical to complex128.
18complex64This is used to represent the complex number where real and imaginary part shares 32 bits each.
19complex128This is used to represent the complex number where real and imaginary part shares 64 bits each.

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'])


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)


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)


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)]) 

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)


[1 2 3]



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.

Follow Us On