Data types ========== https://numpy.org/doc/2.3/user/basics.types.html See also Data type objects Array types and conversions between types NumPy supports a much greater variety of numerical types than Python does. This section shows which are available, and how to modify an array’s data-type. NumPy numerical types are instances of numpy.dtype (data-type) objects, each having unique characteristics. Once you have imported NumPy using import numpy as np you can create arrays with a specified dtype using the scalar types in the numpy top-level API, e.g. numpy.bool, numpy.float32, etc. These scalar types as arguments to the dtype keyword that many numpy functions or methods accept. For example: z = np.arange(3, dtype=np.uint8) z array([0, 1, 2], dtype=uint8) Array types can also be referred to by character codes, for example: np.array([1, 2, 3], dtype='f') array([1., 2., 3.], dtype=float32) np.array([1, 2, 3], dtype='d') array([1., 2., 3.], dtype=float64) See Specifying and constructing data types for more information about specifying and constructing data type objects, including how to specify parameters like the byte order. To convert the type of an array, use the .astype() method. For example: z.astype(np.float64) array([0., 1., 2.]) Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128. The other data-types do not have Python equivalents. To determine the type of an array, look at the dtype attribute: z.dtype dtype('uint8') dtype objects also contain information about the type, such as its bit-width and its byte-order. The data type can also be used indirectly to query properties of the type, such as whether it is an integer: d = np.dtype(np.int64) d dtype('int64') np.issubdtype(d, np.integer) True np.issubdtype(d, np.floating) False Numerical Data Types There are 5 basic numerical types representing booleans (bool), integers (int), unsigned integers (uint) floating point (float) and complex. A basic numerical type name combined with a numeric bitsize defines a concrete type. The bitsize is the number of bits that are needed to represent a single value in memory. For example, numpy.float64 is a 64 bit floating point data type. Some types, such as numpy.int_ and numpy.intp, have differing bitsizes, dependent on the platforms (e.g. 32-bit vs. 64-bit CPU architectures). This should be taken into account when interfacing with low-level code (such as C or Fortran) where the raw memory is addressed. Data Types for Strings and Bytes In addition to numerical types, NumPy also supports storing unicode strings, via the numpy.str_ dtype (U character code), null-terminated byte sequences via numpy.bytes_ (S character code), and arbitrary byte sequences, via numpy.void (V character code). All of the above are fixed-width data types. They are parameterized by a width, in either bytes or unicode points, that a single data element in the array must fit inside. This means that storing an array of byte sequences or strings using this dtype requires knowing or calculating the sizes of the longest text or byte sequence in advance. As an example, we can create an array storing the words "hello" and "world!": np.array(["hello", "world!"]) array(['hello', 'world!'], dtype='