Open In App

Change the Data Type of the Given NumPy Array

Improve
Improve
Like Article
Like
Save
Share
Report

NumPy arrays are homogenous, meaning all elements in a NumPy array are of the same data type and referred to as array type. You might want to change the data type of the NumPy array to perform some specific operations on the entire data set. 

In this tutorial, we are going to see how to change the data type of the given NumPy array. We will use the astype() function of the NumPy library to change the data type of the NumPy array.

NumPy astype() Method

The numpy.astype() method is used to change the data type NumPy array from one data type to another.

The function takes an argument which is the target data type. The function supports all the generic types and built-in types of data.

Syntax

Syntax: ndarray.astype(dtype, order=’K’, casting=’unsafe’, subok=True, copy=True)

Parameters:

  • dtype: The data type you want to change into.
  • order: Controls the memory layout order of the result. Options are ‘C’, ‘F’, ‘A’, ‘K’.
  • casting: Controls the type of data casting. Options are ‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’.
  • subok: If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.
  • copy: By default, astype() returns a newly allocated array. If set to false, the input array is returned instead of a copy.

Return: The method returns a new array with new data type.

Sample Programs

Here we will go through some Python programs to see how we can change data type of the given NumPy array. The problems will help you understand astype() function and the changes in specific data types.

Problem 1

Given a NumPy array whose underlying data is of ‘int32’ type. Change the data type of the given object to ‘float64’.

Solution :

Using the NumPy.astype() function to change the data type of the underlying data of the given NumPy array.

Python3




import numpy as np
# Create a numpy array
arr = np.array([10, 20, 30, 40, 50])
# Print the array
print(arr)


Output :

created NumPy array

Now we will check the data type of the given array object with dtype property.

Python3




# Print the dtype
print(arr.dtype)


Output :

printing NumPy array data type

As we can see in the output, the current data type of the given array object is ‘int32’. Now we will change this to ‘float64’ type.

Python3




# change the dtype to 'float64'
arr = arr.astype('float64')
print(arr)
  
# print the data type
print(arr.dtype)


Output :

printed NumPy arrayConverted NumPy array data type

Problem 2 :

Given a NumPy array whose underlying data is of ‘int32’ type. Change the data type of the given object to ‘complex128’.

Solution :

We will use NumPy.astype() function to change the data type of the underlying data of the given NumPy array.

Python3




import numpy as np
# Create a numpy array
arr = np.array([10, 20, 30, 40, 50])
print(arr)


Output :

printing new array

Now we will check the data type of the given array object.

Python3




# Print the dtype
print(arr.dtype)


Output :

printing NumPy array data type

As we can see in the output, the current data type of the given array object is ‘int32’. Now we will change this to ‘complex128’ type.

Python3




# change the dtype to 'complex128'
arr = arr = arr.astype('complex128')
print(arr)
  
# Also print the data type
print(arr.dtype)


Output :

printing converted NumPy arrayprinting converted NumPy array data type

Conclusion

Converting the data type (type) of a array in NumPy might be useful to perform datatype-specific operations on the entire data set. You can convert the data type of an entire array with the built-in NumPy library function astype().

In this tutorial, we have covered the best way to change the data type of the given NumPy array with astype() method. We have provided an easy explanation for the method and also covered sample problems/examples to provide a better understanding of the concept.



Last Updated : 02 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads