By default Python have these data types:
NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.
Below is a list of all data types in NumPy and the characters used to represent them.
i - integerb - booleanu - unsigned integerf - floatc - complex floatm - timedeltaM - datetimeO - objectS - stringU - unicode stringV - fixed chunk of memory for other type ( void )The NumPy array object has a property called dtype that returns the data type of the array:
Example: Get the data type of an 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)We use the array() function to create arrays, this function can take an optional argument: dtype that allows us to define the expected data type of the array elements:
Example: Create an array with 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 define size as well.
Example: Create an array with data type 4 bytes integer
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='i4')
print(arr)
print(arr.dtype)If a type is given in which elements can't be casted then NumPy will raise a ValueError.
ValueError: In Python ValueError is raised when the type of passed argument to a function is unexpected/incorrect.
Example: A non integer string like 'a' can not be converted to integer (will raise an error)
import numpy as np
arr = np.array(['a', '2', '3'], dtype='i')The best way to change the data type of an existing array, is to make a copy of the array with the astype() method.
The astype() function creates a copy of the array, and allows you to specify the data type as a parameter.
Example: Change data type from float to integer by using 'i' as 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: Change data type from float to integer by using int as 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 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)