NumPy Data Types

NumPy supports far more data types than Python's built-in types, and they basically correspond to C language data types, with some of them corresponding to Python built-in types. The following table lists the commonly used NumPy basic types.

Name Description
bool_ Boolean data type (True or False)
int_ Default integer type (similar to long in C, int32 or int64)
intc Same as C's int type, generally int32 or int64
intp Integer type used for indexing (similar to ssize_t in C, generally still int32 or int64)
int8 Byte (-128 to 127)
int16 Integer (-32768 to 32767)
int32 Integer (-2147483648 to 2147483647)
int64 Integer (-9223372036854775808 to 9223372036854775807)
uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
float_ Abbreviation of float64 type
float16 Half-precision floating point, including: 1 sign bit, 5 exponent bits, 10 mantissa bits
float32 Single-precision floating point, including: 1 sign bit, 8 exponent bits, 23 mantissa bits
float64 Double-precision floating point, including: 1 sign bit, 11 exponent bits, 52 mantissa bits
complex_ Abbreviation of complex128 type, i.e., 128-bit complex number
complex64 Complex number, represented by two 32-bit floating point numbers (real and imaginary parts)
complex128 Complex number, represented by two 64-bit floating point numbers (real and imaginary parts)

NumPy's numeric types are actually instances of dtype objects, each corresponding to a unique character, including np.bool_, np.int32, np.float32, and so on.


Data Type Object (dtype)

Data type objects (instances of the numpy.dtype class) are used to describe how the memory region corresponding to an array is used. They describe the following aspects of the data:

  • The type of data (integer, floating point, or Python object)
  • The size of the data (e.g., how many bytes an integer uses for storage)
  • The byte order of the data (little-endian or big-endian)
  • In the case of structured types, the names of fields, the data type of each field, and the part of the memory block each field takes
  • If the data type is a sub-array, then what its shape and data type are.

Byte order is determined by pre-setting the data type<or>to decide.<Means little-endian (the least significant byte is stored at the smallest address, i.e., the low-order byte group is placed first).>Means big-endian (the most significant byte is stored at the smallest address, i.e., the high-order byte group is placed first).

dtype objects are constructed using the following syntax:

numpy.dtype(object, align, copy)
  • object - the data type object to convert to
  • align - if true, pad the fields to make it similar to a C struct.
  • copy - copy the dtype object; if false, it is a reference to the built-in data type object.

Examples

Next, we can understand through examples.

Example 1

import numpy as np # Using scalar types dt = np.dtype(np.int32) print(dt)

The output result is:

int32

Example 2

import numpy as np # The four data types int8, int16, int32, int64 can be replaced by the strings 'i1', 'i2', 'i4', 'i8' dt = np.dtype('i4') print(dt)

The output result is:

int32

Example 3

import numpy as np # Byte order annotation dt = np.dtype('<i4') print(dt)

The output result is:

int32

The following example demonstrates the use of structured data types; the type fields and the corresponding actual types will be created.

Example 4

# First create a structured data type import numpy as np dt = np.dtype([('age',np.int8)]) print(dt)

The output result is:

[('age', 'i1')]

Example 5

# Apply the data type to an ndarray object import numpy as np dt = np.dtype([('age',np.int8)]) a = np.array([(10,),(20,),(30,)], dtype = dt) print(a)

The output result is:

[(10,) (20,) (30,)]

Example 6

# The type field name can be used to access the actual age column import numpy as np dt = np.dtype([('age',np.int8)]) a = np.array([(10,),(20,),(30,)], dtype = dt) print(a['age'])

The output result is:

[10 20 30]

The following example defines a structured data type student, containing a string field name, an integer field age, and a floating-point field marks, and applies this dtype to an ndarray object.

Example 7

import numpy as np student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) print(student)

The output result is:

[('name', 'S20'), ('age', 'i1'), ('marks', 'f4')]

Example 8

import numpy as np student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student) print(a)

The output result is:

[(b'abc', 21, 50.) (b'xyz', 18, 75.)]

Each built-in type has a unique character code that defines it, as follows:

Character Corresponding type
bBoolean
i(Signed) integer
u Unsigned integer
fFloating point
c Complex floating point
m timedelta (time interval)
M datetime (date and time)
O (Python) object
S, a (byte-)string
U Unicode
V Raw data (void)
Other extensions