What are Numpy data types

There are three broad categories under which data types of Numpy are divided-

  1. Numpy Numeric Types
  2. Numpy String Types
  3. Numpy Boolean Types

Numpy Numeric Types

Numeric types include signed and unsigned integer, floating-point numbers, and complex numbers.

Numpy Numeric Data Types Character Codes Description
Signed Integer
int8 b, i1 or |i1 It is an 8-bit(1 byte) signed integer and its range is -128 to 127.
int16 h, i2 or <i2 It is a 16-bit(2 bytes) signed integer and its range is -32768 to 32767.
int32 i, i4 or <i4 It is a 32-bit(4 bytes) signed integer and its range is -231 to 231 - 1.
int64 l, i8 or <i8 It is a 64-bit(8 bytes) signed integer and its range is -263 to 263 - 1.
Unsigned Integer
uint8 B, u1 or |u1 It is an 8-bit(1 byte) unsigned integer and its range is 0 to 255.
uint16 H, u2 or <u2 It is a 16-bit(2 bytes) unsigned integer and its range is 0 to 65535.
uint32 I, u4 or <u4 It is a 32-bit(4 bytes) unsigned integer and its range is 0 to 232 - 1.
uint64 L, u8 or <u8 It is a 64-bit(8 bytes) unsigned integer and its range is 0 to 264 - 1.
Single and Half Precision Floating-Point Number
float16 e, f2 or <f2 It is a half precision float with signed bit- 5 bits exponent and 10 bits mantissa.
float32 f, f4 or <f4 It is a single precision float with signed bit- 8 bits exponent and 23 bits mantissa.
Double Precision Floating-Point Number
float64 d, f8 or <f8 It is a double precision float with signed bit- 11 bits exponent and 52 bits mantissa.
Quadruple Precision Floating-Point Number
float128 g, f16 or <f16 It is a quadruple precision float with signed bit- 15 bits exponent and 112 bits mantissa.
Complex Number
complex64 F, c8 or <c8 It is a two 32-bit floating complex number
complex128 D, c16 or <c16 It is a two 64-bit floating complex number

Numpy String Types

String types include String and Unicode.

Numpy String Data Types Character Codes Description
S or string_ Sx or |Sx
x is the number of characters.
It is a string data type whose size is equal to x bytes.
U, unicode_ or str_ Ux or <Ux
x is the number of characters.
It is a unicode data type whose size is equal to 4*x bytes.

Numpy Boolean Types

It represents True or False.

Numpy Boolean Data Type Character Codes Description
b b It is a boolean data type that takes either True or False.

Numpy Data Types Character Codes

Character codes are mainly used in two places-

  • You specify the data type of fields using character codes passed to dtype() constructor.
    t = np.dtype([('name', 'S30'), ('age', 'i2')])
  • Character codes are passed to Numpy functions where you specify the data type using dtype parameter.
    arr = np.array([12, 34.65, 98.333, 67], dtype='f4')
    print(arr)
    Output of the above program
    [12.    34.65  98.333 67.   ]

dtype constructor

dtype() constructor is used to create homogeneous and heterogeneous data types. To the constructor, you can pass numpy data types, their character codes, and a list of tuples having fields and their data types.

homogeneous = dtype('float32')

The below code will create record data type that contains product name represented by a 50-character string, its quantity represented by 16-bit integer and its price represented by 16-bit floating point number.

hetero = dtype([('productname', S50), ('quantity', int16), ('price', float16)])

Attributes of dtype

There are three attributes of dtype class-

  1. char- It provides the character code of a data type.
    import numpy as np
    d = np.dtype('float16')
    print(d.char)
    It will give e in the output.
  2. str- It provides the string representation of a data type. The representation starts with '<' then character code, and finally the number of bytes that data type requires.
    import numpy as np
    d = np.dtype('int32')
    print(d.str)
    It will give <i4 in the output.
  3. itemsize- It returns the size of a data type in bytes.
    import numpy as np
    d = np.dtype('int64')
    print(d.itemsize)
    It will give 8 in the output.

Recommended Posts