Convert Data Type of NumPy Arrays
We can convert data type of an arrays from one type to another using astype() function. In the below code we have initialize an array with float type values. After that we have convert that float64 type array to int32 type using astype() function. Finally, print the array and their types of original array and new array.
Python3
import numpy as np # Create an array with float data type elements arr_original = np.array([ 1.2 , 2.5 , 3.7 ]) # Converting to int32 arr_new = arr_original.astype(np.int32) # Print the original its type print ( "Original array:" , arr_original) print ( "Data type of original array:" , arr_original.dtype) # Print new array and its type print ( "\nNew array:" , arr_new) print ( "Data type of new array:" , arr_new.dtype) |
Output:
Numpy data Types
NumPy is a powerful Python library that can manage different types of data. Here we will explore the Datatypes in NumPy and How we can check and create datatypes of the NumPy array.
Contact Us