# NumPy Data Types (Python Tutorial)

This article explains the NumPy Data Types. Learn the data types that are supported by NumPy to categorize different types of data.

## Data Types

Data Types help in referencing a particulate kind of element by what kind of values it takes in and also what kind of operations and functions can be performed on it. The elements are divided into various groups of data types. These Data types differ in their properties and also their functionality.

So some of the default Data Types that are present in python is as follows:

1. `Strings`: These usually represent all the textual data, anything written in alphabetical letters starting from A to Z in the quotes we call them to string. Example: “HELLO”
2. `Integer`: These represent all the numbers. we can write these in both positive as well as negative forms. eg: 1, 2, -5, -9, etc.
3. `Float` : These represent the decimal number or the real numbers. We can have any number of digits after the decimal. eg: 1.1, 2.987, 4.7, etc.
4. `Boolean` : These represent the situation in one of the two forms which can either be True or False.
5. `Complex` : This represents those number which has some variable concerning them and is using them for complex calculations. e.g., 2.0 + 2.5j, 3.5 + 12.5j, etc.

## NumPy Data Types

The Data Types referred up till now are the basic data types in NumPy we have various other data types. So in NumPy, we can categorize data in various forms, and they are a huge variety of them.

Let us go through the list of data types along with the characters that represent them:

• `i` – This presents the integer type.
• `b` – This is boolean (True/False)
• `u` – This represents an unsigned integer.
• `f` – all the float values.
• `c` – Complex Float values.
• `M` – This represents a time delta.
• `O` – objects of the array.
• `S` – strings that include all the alphabets.
• `U` – Unicode strings.
• `V` – This represents that chunk of the memory. As a result, it is usually void.

## Data Type of an Array

Usually, there are many ways to find the type of the array, but NumPy has its own property to check the type of data present in an array. This property with which we can check the type of the array is known as `dtype`. This describes us how bytes in a fixed-size block of memory corresponding to an array item should be interpreted.

It tells us about the following aspects of the array:

• It tells us about the type of data if it is a interfere or a string or anything else.
• Predicts the size of the array in bytes.
• Also tells us about the byte order of the arrays data.
• If the data type is a structured data type.
• If the data type is a sub-array and also provides details about the shape of the array and data type.

The byte order of the arrays in NumPy is decided by fixing the ‘>’ or'<‘ to the data type. Here ‘>’ tells us about encoding in big-endian and ‘<‘ tells us that encoding is little-endian.

Let us check the data type of some arrays:

Output:

`int64`

Here we are trying to get the type of data that is an integer.

Let us take another example for alphabets:

```#here we are importing the numpy and also making alias as np
import numpy as np
#we created an array with some odd numbers
array1=np.array(['car','bus','van'])
# now we will check the data type of the array
print(array1.dtype)```

Output:

`<U3`

Here we will get the data type of the strings and also if it is little-endian or big-endian.

## Creating Arrays with `dtype`

In order to create arrays along with using `dtype`  we can use it as an optional argument where we would specify the type of data we want it to convert it into and then display.

Let us take an example:

```import numpy as np
array1=np.array([1,3,5,7,9], dtype='S')

print( array1, array1.dtype)```

Output:

```[b'1' b'3' b'5'b'7'b'9']
|S1```

here it would convert the data into the desired datatype.

## Conversion on Existing Arrays

NumPy arrays have their own method of converting the data types of already existing arrays. This method is known as `astype()` which helps us in creating a copy of the arrays and allows us to change the data type of the array.

Let us go through an example to understand it better:

Output:

So we get the conversion in this type of the array along with the data type that it will convert into. But we have certain restrictions as well.

We can convert the array which has elements of more than two types; it will end up showing errors. ValueError will rise when the type values pass in the array, which is totally unexpected and incorrect.

Also, we should be very careful with the type in which we want our elements to be converted.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!