NumPy DataType Conversion

In this tutorial you will learn

  • How to check DataType?
  • How to convert one DataType to another?
  • Unconvertible DataTypes

Check Data Type

As discussed in previous tutorial, NumPy provides multiple inbuilt DataTypes, so inorder to manipulate the data structure with a particular data type we first have to ensure that we are dealing with the right Data/ DataType. There are two options, either we can declare the DataType of Data Structure while initializing it or we can initialize data structure without declaring the DataType and later on check the data Type to ensure we are working with the right Data Type. So in order to check the data type NumPy provides a built in property (associated with each Data object) called “dtype”. In examples below I will demonstrate that how can we check the Data type of an array.

  1. import numpy as np
  2. my_arr = np.array([1j,2,2+3j])
  3. print('The data type of Array is:', my_arr.dtype)
  1. import numpy as np
  2. my_arr = np.array([90,200,44,-152])
  3. print('The data type of Array is:', my_arr.dtype)
  1. import numpy as np
  2. my_arr = np.array(['source', 'codester', 'NumPy','Tutorials'])
  3. print('The data type of Array is:', my_arr.dtype)
  1. import numpy as np my_arr = np.array(['1', '2', '3','4'])
  2. print('The data type of Array is:', my_arr.dtype)

Convert Data Types

Some of the data types are inter convertible. There is an inbuilt function ‘astype’ which can be used to inter convert the array to different Data Types. This function creates a copy of your original Array and we can assign the new array another name. In this example we will be converting the integer data type into float

  1. import numpy as np
  2. my_arr = np.array([7, 6, 5, 4, 3, 2, 1]
  3. arr = my_arr.astype('f') print('Converted Array is:', arr)
  4. print('Converted Data Type is:', arr.dtype)

In this example we will be converting the integer data type into String Data Type

  1. import numpy as np
  2. my_arr = np.array([7, 6, 5, 4, 3, 2, 1])
  3. arr = my_arr.astype('S')
  4. print('Converted Array is:', arr)
  5. print('Converted Data Type is:', arr.dtype)

Unconvertible Data Types

In this example I am trying to convert a String Data Type into float Data Type but this will give an error as there are some built in limitations. Numeric values can be converted to string but non numeric values cannot be converted in numeric values as demonstrated below

  1. import numpy as np
  2. my_arr = np.array(['Alpha', 'Bravo', 5, 4, 3, 2, 1])
  3. arr = my_arr.astype('f') print('Converted Array is:', arr)
  4. print('Converted Data Type is:', arr.dtype)

In this example I am trying to convert an Complex Data Type Array into Boolean, although the program compiles successfully but the compiler automatically discards the imaginary part of the complex no and simply converts the real part to boolean.

  1. import numpy as np
  2. my_arr = np.array([ 5+3j, 4+4J, 3-1j, 2, 1])
  3. arr = my_arr.astype('b') print('Converted Array is:', arr)
  4. print('Converted Data Type is:', arr.dtype)

Add new comment