codingstreets
Search
Close this search box.
python-numpy-data-types

Introduction to Python Numpy Data Types

In This Article, You Will Learn About Python Numpy Data Types.

Python Numpy Data Types – Before moving ahead, let’s know a little bit about Python NumPy Slicing

These data types default to Python

String – Used to represent text data, the text will be given under quotation marks.

Integer – Used to represent integer numbers.

Float – Used to represent real numbers

Boolean – Used to indicate True or False

Complex number – Used to represent complex numbers.

NumPy Data Types

NumPy also supports additional data types. These data types can be referred to with just one character such as f for float or O for the object.

Below is a listing of all data types available in NumPy and the characters that 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 for other types of memory

Verifying the Data Type of an Array

A property of the NumPy array object is dtype which returns the data type for the array.

Example – Returning the data type for an array object.

import numpy as np
Array = np.array([1, 2, 3, 4])

print(Array.dtype)
Output - 

int32

To sum up, it returned the data type of an array.

Example – Returning the data type for an array object.

import numpy as np
Array = np.array([1+2j, 2+1j, 3+7j, 4+2j])

print(Array.dtype)
Output - 

complex128

In conclusion, it returned the data type of an array.

Create Arrays with a Defined Type of Data

To create arrays, we use the array() function. This function can accept an optional argument: the argument dtype. This allows us to specify the expected data type for the array elements.

Example – Creating an array with data type float.

import numpy as np
Array = np.array([1, 2, 3, 4], dtype='f')

print(Array)
print(Array.dtype)
Output -

[1. 2. 3. 4.]
float32

Overall, it returned an array with specified data type float.

Example – Creating an array with data type 8 bytes integer.

import numpy as np
Array = np.array([1, 2, 3, 4,5,6], dtype='i8')

print(Array)
print(Array.dtype)
Output   -

[1 2 3 4 5 6]
int64

All things considered, it returned an array with data type 8 bytes integer.

Note: Data types – i, u, f, S, and U can be defined with size as well.

What happens if a value cannot be converted?

NumPy will raise an error if a type contains elements that can’t be cast.

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

An error will be raised if a non-integer string, such as ‘k’, is converted to an integer.

Example – An error will be raised if a non-integer string will be converted.

import numpy as np
Array = np.array(['k', '2', '3'], dtype='i')

print (Array)
Output - 

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

As shown above, it returned an error cause of conversion of non-integer string.

Convert Data Types on Existing Arrays

You can change the data type of an array by making a copy using the astype() method.

astype() creates a copy from the array and allows you to specify the data type as parameters.

You can specify the data type using a string, like ‘c’ for complex float, ‘b’ for Boolean valuer, etc. You can also use the data type directly, such as int for integer.

Example – Changing data type from integer to float by using ‘f’ as parameter value:

import numpy as np

Array = np.array([8, 29, 31])
Array_type_float = Array.astype('f')

print(Array_type_float)
print(Array_type_float.dtype)
Output - 

[ 8. 29. 31.]
float32

In the final analysis, it returned the changed data type of an array.

Example – Changing data type from float to integer by using ‘i’ as the parameter value.

import numpy as np

Array = np.array([1.1, 2.1, 8.1])
New_Array = Array.astype('i')

print(Array)
print(New_Array.dtype)
python-numpy-data-types

As can be seen, it returned the changed data type of an array.

Example – Changing data type from complex to integer numbers.

import numpy as np

Array = np.array([1+1j, 2+6j, 8+1j])
New_Array = Array.astype('i')

print(New_Array)
print(New_Array.dtype)
Output - 

[1 2 8]
int32

As a result, it returned the changed data type of array i,e., integer data type.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on