
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Change data type of given numpy array in Python
We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array.
We can check the type of numpy array using the dtype class. Let's check the data type of sample numpy array.
Example
# importing numpy library import numpy as np # creating numpy array array = np.array([1, 2, 3, 4, 5]) # printing the data type of the numpy array print(array.dtype)
Output
If you run the above code, you will get the following results.
int32
Let's see how to change the data type of a numpy array from float64 to &int32.
Example
# importing numpy library import numpy as np # creating numpy array of type float64 array = np.array([1.5, 2.6, 3.7, 4.8, 5.9]) # type of array before changing print(f'Before changing {array.dtype}') # changing the data type of numpy array using astype() method array = array.astype(np.int32) # type of array after changing print(f'\nAfter changing {array.dtype}')
Output
If you run the above program, you will get the following results.
Before changing float64 After changing int32
We can use any data type present in the numpy module or general data types of Python. You can find the list of data types present in numpy here.
Conclusion
I hope you have learned the conversion of data types for numpy array. If you are facing any problems related to the tutorial, mention them in the comment section.
- Related Articles
- Change data type of given numpy array
- Convert the input to a masked array of the given data-type in Numpy
- Return a new array of given shape without initializing entries and change the default type in Numpy
- Return a new array with the same shape as a given array but change the default type in Numpy
- Return a new array with the same shape and type as a given array and change the order in Numpy
- Return a new array of given shape filled with ones and also set the desired data-type in Numpy
- Return a new array of given shape and type, filled with array-like in Numpy
- Return a new array of given shape and type without initializing entries in Numpy
- Return a new array with the same shape and type as a given array and change the order to K style in Numpy
- Return a new array with the same shape and type as a given array and change the order to C style in Numpy
- Return an array of ones with the same shape and type as a given array in Numpy
- Return an array of zeros with the same shape and type as a given array in Numpy
- Return a new array with the same shape and type as given array in Numpy
- How to change any data type into a string in Python?
- Find the minimal data type of an array-like in Python
