- Python Data Analysis
- Ivan Idris
- 761字
- 2025-04-04 21:30:24
NumPy numerical types
Python has an integer type, a float type, and complex type; nonetheless, this is not sufficient for scientific calculations. In practice, we still demand more data types with varying precisions and, consequently, different storage sizes of the type. For this reason, NumPy has many more data types. The bulk of the NumPy mathematical types ends with a number. This number designates the count of bits related to the type. The following table (adapted from the NumPy user guide) presents an overview of NumPy numerical types:

For each data type, there exists a matching conversion function (look at the numericaltypes.py
script of this book's code bundle):
In: float64(42) Out: 42.0 In: int8(42.0) Out: 42 In: bool(42) Out: True In: bool(0) Out: False In: bool(42.0) Out: True In: float(True) Out: 1.0 In: float(False) Out: 0.0
Many functions have a data type argument, which is frequently optional:
In: arange(7, dtype=uint16) Out: array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)
It is important to be aware that you are not allowed to change a complex number into an integer. Attempting to do that sparks off a TypeError
:
In: float(42.0 + 1.j) Traceback (most recent call last): File "numericaltypes.py", line 45, in <module> print float(42.0 + 1.j) TypeError: can't convert complex to float
The same goes for conversion of a complex number into a floating-point number. By the way, the j
component is the imaginary coefficient of a complex number. Even so, you can convert a floating-point number to a complex number, for example, complex(1.0)
. The real and imaginary pieces of a complex number can be pulled out with the real()
and imag()
functions, respectively.
Data type objects
Data type objects are instances of the numpy.dtype
class. Once again, arrays have a data type. To be exact, each element in a NumPy array has the same data type. The data type object can tell you the size of the data in bytes. The size in bytes is given by the itemsize
property of the dtype
class (refer to dtypeattributes.py
):
In: a.dtype.itemsize Out: 8
Character codes
Character codes are included for backward compatibility with Numeric. Numeric is the predecessor of NumPy. Its use is not recommended, but the code is supplied here because it pops up in various locations. You should use the dtype
object instead. The following table lists several different data types and character codes related to them:

Take a look at the following code to produce an array of single precision floats (refer to charcodes.py
in this book's code bundle):
In: arange(7, dtype='f') Out: array([ 0., 1., 2., 3., 4., 5., 6.], dtype=float32)
Likewise, this creates an array of complex numbers:
In: arange(7, dtype='D') Out: array([ 0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j])
The dtype constructors
We have a variety of means to create data types. Take the case of floating-point data (have a look at dtypeconstructors.py
in this book's code bundle):
- We can use the general Python float, as shown in the following lines of code:
In: dtype(float) Out: dtype('float64')
- We can specify a single precision float with a character code:
In: dtype('f') Out: dtype('float32')
- We can use a double precision float with a character code:
In: dtype('d') Out: dtype('float64')
- We can pass the
dtype
constructor a two-character code. The first character stands for the type; the second character is a number specifying the number of bytes in the type (the numbers 2, 4, and 8 correspond to floats of 16, 32, and 64 bits, respectively):In: dtype('f8') Out: dtype('float64')
A (truncated) list of all the full data type codes can be found by applying sctypeDict.keys()
:
In: sctypeDict.keys() Out: [0, … 'i2', 'int0']
The dtype attributes
The dtype
class has a number of useful properties. For instance, we can get information about the character code of a data type through the properties of dtype
(refer to dtypeattributes2.py
in this book's code bundle):
In: t = dtype('Float64') In: t.char Out: 'd'
The type attribute corresponds to the type of object of the array elements:
In: t.type Out: <type 'numpy.float64'>
The str
attribute of dtype
gives a string representation of a data type. It begins with a character representing endianness, if appropriate, then a character code, succeeded by a number corresponding to the number of bytes that each array item needs. Endianness, here, entails the way bytes are ordered inside a 32- or 64-bit word. In the big-endian order, the most significant byte is stored first, indicated by >
. In the little-endian order, the least significant byte is stored first, indicated by <
, as exemplified in the following lines of code:
In: t.str Out: '<f8'