NumPy Data Types
- Previous Page NumPy Array Slicing
- Next Page NumPy Copies/Views
Data types in Python
By default, Python has the following data types:
strings
- Used to represent text data, which is enclosed in quotes. For example "ABCD".integer
- Used to represent integers. For example -1, -2, -3.float
- Used to represent real numbers. For example 1.2, 42.42.boolean
- Used to represent True or False.complex
- Used to represent numbers in the complex plane. For example 1.0 + 2.0j, 1.5 + 2.5j.
Data types in NumPy
NumPy has some additional data types, and data types are referenced by a character, for example i
Representing integers,u
Representing unsigned integers, etc.
The following is a list of all data types in NumPy and the characters used to represent them.
i
- Integerb
- Booleanu
- Unsigned integerf
- Floating-pointc
- Complex floating-point numberm
- TimedeltaM
- DatetimeO
- ObjectS
- StringU
- Unicode stringV
- Fixed memory block of other types (void)
Check the data type of the array
NumPy array objects have a named dtype
The attribute, which returns the data type of the array:
Example
Get the data type of the array object:
import numpy as np arr = np.array([1, 2, 3, 4]) print(arr.dtype)
Example
Get the data type of an array containing strings:
import numpy as np arr = np.array(['apple', 'banana', 'cherry']) print(arr.dtype)
to create an array with a predefined data type
We use array()
The function to create an array, which can use optional parameters:dtype
It allows us to define the expected data type of array elements:
Example
Create an array using a data type string:
import numpy as np arr = np.array([1, 2, 3, 4], dtype='S') print(arr) print(arr.dtype)
For i
,u
,f
,S
and U
We can also define the size.
Example
Create an array with data type of 4-byte integer:
import numpy as np arr = np.array([1, 2, 3, 4], dtype='i4') print(arr) print(arr.dtype)
What if the value cannot be converted?
If the type of the element cannot be forced to convert, NumPy will raise a ValueError.
ValueError: In Python, if the type of the parameter passed to the function is not expected or incorrect, ValueError will be raised.
Example
ValueError: In Python, if the type of the parameter passed to the function is unexpected or incorrect, ValueError will be raised.
import numpy as np arr = np.array(['a', '2', '3'], dtype='i')
Convert the data type of an existing array
The best way to change the data type of an existing array is to use astype()
method copies the array.
astype()
The function creates a copy of the array and allows you to specify the data type as a parameter.
Data types can be specified as strings, for example 'f'
It represents a float,'i'
You can also use data types directly, for example float
It represents a float,int
It represents an integer.
Example
By using 'i'
Change the data type from float to integer as a parameter value:
import numpy as np arr = np.array([1.1, 2.1, 3.1]) newarr = arr.astype('i') print(newarr) print(newarr.dtype)
Example
By using int
Change the data type from float to integer as a parameter value:
import numpy as np arr = np.array([1.1, 2.1, 3.1]) newarr = arr.astype(int) print(newarr) print(newarr.dtype)
Example
Change the data type from integer to boolean:
import numpy as np arr = np.array([1, 0, 3]) newarr = arr.astype(bool) print(newarr) print(newarr.dtype)
- Previous Page NumPy Array Slicing
- Next Page NumPy Copies/Views