Change data type of given numpy array - GeeksforGeeks

23 Jun, 2021
In this post, we are going to see the ways in which we can change the dtype of the given numpy array. In order to change the dtype of the given array object, we will use numpy.astype() function. The function takes an argument which is the target data type. The function supports all the generic types and built-in types of data.
YouTubeGeeksforGeeks502K subscribersHow to Change the Data Type of NumPy Array? | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:28•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=NNl--RW75AM" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Problem #1 : Given a numpy array whose underlying data is of 'int32' type. Change the dtype of the given object to 'float64'.
Solution : We will use numpy.astype() function to change the data type of the underlying data of the given numpy array.
# importing the numpy library as npimport numpy as np  # Create a numpy arrayarr = np.array([10, 20, 30, 40, 50])  # Print the arrayprint(arr)
Output :
Now we will check the dtype of the given array object.
# Print the dtypeprint(arr.dtype)
Output :
As we can see in the output, the current dtype of the given array object is ‘int32’. Now we will change this to ‘float64’ type.
# change the dtype to 'float64'arr = arr.astype('float64')  # Print the array after changing# the data typeprint(arr)  # Also print the data typeprint(arr.dtype)
Output :
Problem #2 : Given a numpy array whose underlying data is of 'int32' type. Change the dtype 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.
# importing the numpy library as npimport numpy as np  # Create a numpy arrayarr = np.array([10, 20, 30, 40, 50])  # Print the arrayprint(arr)
Output :
Now we will check the dtype of the given array object.
# Print the dtypeprint(arr.dtype)
Output :
As we can see in the output, the current dtype of the given array object is ‘int32’. Now we will change this to ‘complex128’ type.
# change the dtype to 'complex128'arr = arr = arr.astype('complex128')  # Print the array after changing# the data typeprint(arr)  # Also print the data typeprint(arr.dtype)
Output :
Python numpy-arrayManipulation
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Python program to convert a list to string
Reading and Writing to text files in Python