لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science NumPy Data Types

Data Types in Python

By default Python have these data types.

Strings are used to represent text data, the text is given under single or double quote marks. e.g. 'abc', "ABCD".

Integer are used to represent integer numbers. e.g. -1, -2, -3.

Float are used to represent real numbers. e.g. 1.5, 42.52.

Boolean are used to represent True or False.

Complex are used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j.

Data Types in NumPy

NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.

Below is a list of all data types in NumPy and the characters used to represent them.

i - integer

b - boolean

u - unsigned integer

f - float

c - complex float

m - timedelta

M - datetime

O - object

S - string

U - unicode string

V - fixed chunk of memory for other type ( void )

Checking the Data Type of an Array

The NumPy array object has a property called dtype which returns the data type of the array.

Example 1: Get the data type of an array object.

Code

import numpy as np

arr = np.array([5, 6, 7, 8])

print(arr.dtype)

the output will be

int32

Example 2: Get the data type of an array containing strings.

Code

import numpy as np

arr = np.array(['apple', 'mango', 'orange'])

print(arr.dtype)

the output will be

U6

Creating Arrays With A Defined Data Type

The array() function is used to create arrays, this function can take an optional argument, dtype that allows us to define the expected data type of the array elements.

Example 3: Create an array with data type string.

Code

import numpy as np

arr = np.array([1, 2, 3, 4], dtype='S')

print(arr)
print(arr.dtype)

the output will be

[b'1' b'2' b'3' b'4']
|S1

Note: For i, u, f, S and U we can define size as well.

Example 4: Create an array with data type 4 bytes integer.

Code

import numpy as np

arr = np.array([2, 3, 4, 5], dtype='i4')

print(arr)
print(arr.dtype)

the output will be

[2 3 4 5]
int32

What happens if a Value Can Not Be Converted?

If a type is given in which elements can't be casted then NumPy will raise a ValueError.

ValueError

In Python ValueError is raised when the type of passed argument to a function is unexpected/incorrect.

Example 5: A non integer string like 'a' can not be converted to integer (It will raise an error).

Code

import numpy as np

arr = np.array(['a', '2', '3'], dtype='i')

the output will be

ValueError: invalid literal for int() with base 10: 'a'

Converting Data Type On Existing Arrays

The ideal way to change the data type of an existing array, is to make a copy of the array with the astype() method.

The astype() function creates a copy of the array, and allows you to specify the data type as a parameter.

The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float for float and int for integer.

Example 6: Change data type from float to integer by using 'i' as parameter value.

code

import numpy as np

arr = np.array([2.1, 3.2, 4.2])

newarr = arr.astype('i')

print(newarr)
print(newarr.dtype)

the output will be

[2 3 4]
int32

Example 7: Change data type from float to integer by using int as parameter value.

Code

import numpy as np

arr = np.array([2.1, 4.1, 5.1])

newarr = arr.astype(int)

print(newarr)
print(newarr.dtype)

the output will be

[2 4 5]
int32

Example 8: Change data type from integer to boolean.

Code

import numpy as np

arr = np.array([2, 0, 1])

newarr = arr.astype(bool)

print(newarr)
print(newarr.dtype)

the output will be

[ True False True]
bool