How to change datatype of existing numpy array

In this post, we will learn How to change the datatype of the existing numpy array. Sometimes we have a numpy array with data in which we have to change the data type of the existing numpy by using numpy.astype() function.The astype() function takes datatype as an argument that we have to change the existing numpy array.

1.Change datatype of existing numpy array


In this Python program example, we have used numpy library numpy.astype() function to change the data type of an existing array. We can import the numpy library in our program by using the code “import numpy as np”.Make sure the numpy library is already installed on our system.

  • The existing dataype of given numpy array is ‘float’.We can check datatype using by dtype attribute of numpy array.
  • To change the datatype of existing numpy array we have used numpy.astype() function passed datatype int as argument.
  • To change the datatype of numpy array in-place,we have passed copy=false as second argument to numpy.astype() function.
import numpy as np
nparr = np.array([[3,6,9,12],[12,15,18,21]],dtype='float')

print('existing array datype:\n',nparr.dtype)

nparr = nparr.astype(int,copy=False)

print('array datatype after change:\n',nparr.dtype)

Output

existing array datype:
 float64
array datatype after change:
 int32
character usedDatatype
irepresent an integer data type
bused for boolean datatype
uused for unsigned integer
ffloat type
ccomplex float
mtime delta
MDatetime
Oobject
Sstring
UUnicode string
complex128complex type
NumPy Datatype

2.Change datatype of existing numpy array float to complex type


In this Python program example, we have to change the existing numpy array float to a complex type.

  • The existing datype of given numpy array is ‘float’.We can check datatype using by dtype attribute of numpy array.
  • To change the datatype of existing numpy array we have used numpy.astype() function passed datatype ‘complex128’ as agrument.
  • To change the dattype of numpy array in-place,we have passed copy=false as second argument to numpy.astype() function
import numpy as np
nparr = np.array([[3,6,9,12],[12,15,18,21]],dtype='float')

print('existing array datype:\n',nparr.dtype)

nparr = nparr.astype('complex128',copy=False)

print('array datatype after change:\n',nparr.dtype)

Output

existing array datype:
 float64
array datatype after change:
 complex128

3.Change datatype of existing numpy array float to unsigned string


In this Python program example, we are changing the existing numpy array float datatype to an unsigned string.

  • The existing datype of given numpy array is ‘float’.we can check datatype using dtype attribute of numpy array.
  • To change the datatype of existing numpy array we have used numpy.astype() function passed datatype as unsigned string..
  • To change the dattype of numpy array in-place,we have passed copy=false as second argument to numpy.astype() function
import numpy as np
nparr = np.array([[3,6,9,12],[12,15,18,21]],dtype='float')

print('existing array datype:\n',nparr.dtype)

nparr = nparr.astype('U',copy=False)

print('array datatype after change:\n',nparr.dtype)
print(nparr)

Output

existing array datype:
 float64
array datatype after change:
 <U32
[['3.0' '6.0' '9.0' '12.0']
 ['12.0' '15.0' '18.0' '21.0']]

4.Change datatype of existing numpy array float to object


This Python program example is how to change the data type of existing numpy array float to object. We can change the datatype of the numpy array using the np.astype() function, passed datatype object.

To change the data type of the existing array in-place without returning a new array bypassing the second argument copy=False

import numpy as np
nparr = np.array([[3,6,9,12],[12,15,18,21]],dtype='float')

print('existing array datype:\n',nparr.dtype)

nparr = nparr.astype('O',copy=False)

print('array datatype after change:\n',nparr.dtype)
print(nparr)

Output

existing array datype:
 float64
array datatype after change:
 object
[[3.0 6.0 9.0 12.0]
 [12.0 15.0 18.0 21.0]]

Summary

In this post, we have learned how to change datatype of existing numpy array by using the numpy library built-in function astype(). In this post, We have a table of numpy datatype bypassing any of this datatype we can change the datatype of numpy array. We can changed float datatype of numpy array to int,unsigned string,object,complex type.