NumPy supports a much greater variety of numerical types than Python does. The following table shows different scalar data types defined in NumPy.
| Sr.No. | Data Types | Description |
|---|---|---|
| 1 | bool_ | Boolean (True or False) stored as a byte |
| 2 | int_ | Default integer type (same as C long; normally either int64 or int32) |
| 3 | intc | Identical to C int (normally int32 or int64) |
| 4 | intp | Integer used for indexing (same as C ssize_t; normally either int32 or int64) |
| 5 | int8 | Byte (-128 to 127) |
| 6 | int16 | Integer (-32768 to 32767) |
| 7 | int32 | Integer (-2147483648 to 2147483647) |
| 8 | int64 | Integer (-9223372036854775808 to 9223372036854775807) |
| 9 | uint8 | Unsigned integer (0 to 255) |
| 10 | uint16 | Unsigned integer (0 to 65535) |
| 11 | uint32 | Unsigned integer (0 to 4294967295) |
| 12 | uint64 | Unsigned integer (0 to 18446744073709551615) |
| 13 | float_ | Shorthand for float64 |
| 14 | float16 | Half precision float: sign bit, 5 bits exponent, 10 bits mantissa |
| 15 | float32 | Single precision float: sign bit, 8 bits exponent, 23 bits mantissa |
| 16 | float64 | Double precision float: sign bit, 11 bits exponent, 52 bits mantissa |
| 17 | complex_ | Shorthand for complex128 |
| 18 | complex64 | Complex number, represented by two 32-bit floats (real and imaginary components) |
| 19 | complex128 | Complex number, represented by two 64-bit floats (real and imaginary components) |
NumPy numerical types are instances of dtype (data-type) objects, each having unique characteristics. The dtypes are available as np.bool_, np.float32, etc.
Data Type Objects (dtype)
A data type object describes interpretation of fixed block of memory corresponding to an array, depending on the following aspects:
- Type of data (integer, float or Python object)
- Size of data
- Byte order (little-endian or big-endian)
- In case of structured type, the names of fields, data type of each field and part of the memory block taken by each field.
- If data type is a subarray, its shape and data type
The byte order is decided by prefixing < or > to data type. < means that encoding is little-endian (least significant is stored in smallest address). > means that encoding is big-endian (most significant byte is stored in smallest address).
A dtype object is constructed using the following syntax:
The parameters are:
- Object: To be converted to data type object
- Align: If true, adds padding to the field to make it similar to C-struct
- Copy: Makes a new copy of dtype object. If false, the result is reference to builtin data type object
Example: Using Array-scalar Type
Output:
Example: Using Equivalent String for Data Type
Output:
Example: Using Endian Notation
Output:
Example: Creating a Structured Data Type
Output:
Example: Applying Structured Data Type to ndarray
Output:
Example: Accessing Field Content of Structured Data Type
Output:
Example: Defining a Complex Structured Data Type
Output:
Example: Applying Complex Structured Data Type to ndarray
Output:
Character Codes
Each built-in data type has a character code that uniquely identifies it.
'b'− boolean'i'− (signed) integer'u'− unsigned integer'f'− floating-point'c'− complex-floating point'm'− timedelta'M'− datetime'O'− (Python) objects'S', 'a'− (byte-)string'U'− Unicode'V'− raw data (void)
Checking the Data Type of an Array
You can check the data type of an array using the dtype attribute. This attribute returns a dtype object, which describes the type of elements in the array:
Output:
Create Arrays With Defined Data Type
In NumPy, you can explicitly specify the data type (dtype) of the elements in an array at the time of its creation.
We can use the dtype parameter in array creation functions (such as np.array(), np.zeros(), np.ones(), etc.) to define the data type of the array elements.
Example: Creating an Integer Array
Output:
Example: Creating a Complex Array
Output:
Convert Data Type of NumPy Arrays
NumPy provides several methods to convert the data type of arrays:
- astype() Method: It is the most commonly used method for type conversion.
- numpy.cast() Functions: A set of functions provided by NumPy for casting arrays to different types.
- In-place Type Conversion: Convert types directly while creating arrays.
Example: Using the "astype" Method
Output:
Example: Using "numpy.int32" Function
Output:
Example: In-place Type Conversion
Output:
What if a Value Cannot Be Converted?
When converting data types, you may encounter values that cannot be converted. Let us explore different scenarios.
Scenario 1: Converting Non-numeric Strings to Numbers
Output:
Scenario 2: Converting Out-of-Range Numbers
Output:
Scenario 3: Converting Complex Numbers to Real Numbers
Output:
Scenario 4: Handling Conversion Errors
Output:
Scenario 5: Using "np.nan" for Invalid Conversions
Output:
Converting Data Type on Existing Arrays
You can also convert the data type of existing arrays using the view() method to change the interpretation of the data without changing the underlying bytes.
Example
Output: