In this post, we will learn about different data types in numpy and also we will see how we can convert data types of numpy array with detailed explanations and examples.

You can also check out our previous article 👉 Create a NumPy Array with Random Values

So, let’s start learning. First, we will explore all the data types of NumPy, and after that, we will try them with examples.

## Data Types in NumPy

Numpy also provides a feature to represent all the data types using their special characters. So let’s explore all the special characters used to represent data types in NumPy.

## List of Characters that are Used to Represent Data Types in NumPy

• `i` – integer
• `b` – boolean
• `u` – unsigned integer
• `f` – float
• `c` – complex float
• `m` – timedelta
• `M` – datatime
• `O` – object
• `S` – String
• `U` – Unicode String
• `V` – void (fixed chunk of memory for other types)

## Check data types of NumPy Array

To check the data type of a numpy array, you can use the `dtype` attribute.

``````import numpy as np

arr = np.array([1,2,3,4,5])
print(arr.dtype)``````

Output:

``int32``

## Create a NumPy Array with Specified Data Types

To create a NumPy array with a specified data type, you need to pass the `dtype` attribute as an argument inside the `array()` function.

``````import numpy as np

arr2 = np.array([1,2,3,4,5], dtype="int8")
print(arr2)
print(arr2.dtype)``````

Output:

``````[1 2 3 4]
int8``````

In NumPy, each data type has a corresponding built-in function, such as `numpy.int8()` for `int8`. You can also use these built-in functions in the `dtype` attribute to create arrays with specified data types.

``````import numpy as np

arr3 = np.array([2,4,6,8,10], dtype=np.int8)
print(arr3)
print(arr3.dtype)``````

Output:

``````[ 2  4  6  8 10]
int8``````

There is one more way to specify the `dtype`, which is to use special characters for data types.

``````import numpy as np

arr4 = np.array([1,2,3,4,5,6,7], dtype="f")
print(arr4)
print(arr4.dtype)``````

Output:

``````[1. 2. 3. 4. 5. 6. 7.]
float32``````

## Convert Data Types of a Numpy Array

There is two ways to convert data types of an array:

1. Using built-in functions &
2. Using `astype()` method

### Using Built-in Function

As we have already discussed, each data type in NumPy has a corresponding function. We can use these functions to convert one data type into another.

#### Int → Float

``````import numpy as np

a1_int = np.array([1,2,3,5,7,11,13,17])
print(a1_int.dtype) # Output: int32
print(a1_int)

print()

# Convert int --> float

a1_float = np.float16(a1_int)
print(a1_float.dtype) # Output: float16
print(a1_float)``````

Output:

``````int32
[ 1  2  3  5  7 11 13 17]

float16
[ 1.  2.  3.  5.  7. 11. 13. 17.]``````

#### Float → Int

``````import numpy as np

a2_float = np.array([1,2,3,5,7,11,13,17], dtype="float32")
print(a2_float.dtype) # Output: float64
print(a2_float)

print()

# Convert float --> int
a2_int = np.int16(a2_float)
print(a2_int.dtype)
print(a2_int)``````

Output:

``````float32
[ 1.  2.  3.  5.  7. 11. 13. 17.]

int16
[ 1  2  3  5  7 11 13 17]``````

### Using `astype()` method

The `astype()` function used to convert data types of an array. It takes the target data type as an argument, and it returns a new array with the same data but in the specified data type.

``````arr = np.array([1,2,3,4,5])
new_arr = arr.astype("float32")
print(new_arr.dtype) # Output: float32
print(new_arr)``````

Output:

``````float32
[1. 2. 3. 4. 5.]`````` Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.