Data Types in Python
strings: used to represent text data under quote marks like "ABCD".
integer: used to represent integer numbers like 1, 2, 3, -1, -2, -3.
float: used to represent real numbers like 1.2, 42.42.
boolean: used to represent True or False.
complex: used to represent complex numbers like 1.0 + 2.0j, 1.5 + 2.5j .
Data Types in NumPy
NumPy has some extra data types and refers them with one character, like
i
for integers,u
for unsigned integers etc. List of all data types in NumPy and the characters used to represent them is as follows:
Checking the Data Type of an Array
The NumPy array object has a property called dtype
that returns the data type of the array.
Program 1: Get the data type of an array object.
import numpy as np arr = np.array([1, 2, 3, 4]) print("The data type of arr is:", arr.dtype)
Output:
The data type of arr is: int32
Program 2: Get the data type of an array containing strings.
import numpy as np arr = np.array(['apple', 'banana', 'cherry']) print("The data type of arr is:", arr.dtype)
Output:
The data type of arr is: <U6
Creating Arrays With a Defined Data Type
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.
Program 3: Create an array with data type string.
import numpy as np arr = np.array([1, 2, 3, 4], dtype='S') print("The value of arr is", arr) print("The data type of arr is:", arr.dtype)
Output:
The value of arr is [b'1' b'2' b'3' b'4'] The data type of arr is: |S1
For
i
,u
,f
,S
andU
we can define size as well.Program 4: Create an array with data type 4 bytes integer.
import numpy as np arr = np.array([1, 2, 3, 4], dtype='i4') print("The value of arr is:", arr) print("The data type of arr is:", arr.dtype)
Output:
The value of arr is: [1 2 3 4] The data type of arr is: int32
What if a Value Can Not Be Converted?
If a type is given in which elements can't be casted then NumPy will raise a ValueError
. In Python, ValueError
is raised when the type of passed argument to a function is unexpected/incorrect.
Program 5: 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') print("The value of arr is:", arr)
Output:
ValueError Traceback (most recent call last) <ipython-input-5-bc5f30a52b19> in <module> 1 import numpy as np ----> 2 arr = np.array(['a', '2', '3'], dtype='i') 3 print(arr) ValueError: invalid literal for int() with base 10: 'a'
Converting Data Type on Existing Arrays
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 us to specify the data type as a parameter. The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or we can use the data type directly like float for float and int for integer.
Program 6: 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("The value of newarr is:", newarr) print("The data type of newarr is:", newarr.dtype)
Output:
The value of newarr is: [1 2 3] The data type of newarr is: int32
Program 7: 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("The value of newarr is:", newarr) print("The data type of newarr is:", newarr.dtype)
Output:
The value of newarr is: [1 2 3] The data type of newarr is: int32
Program 8: Change data type from integer to boolean.
import numpy as np arr = np.array([1, 0, 3]) newarr = arr.astype(bool) print("The value of newarr is:", newarr) print("The data type of newarr is:", newarr.dtype)
Output:
The value of newarr is: [ True False True] The data type of newarr is: bool
Usefull information sir
Thank u sir