Data Types In Numerical Python

In this article we discuss numpy data types with examples to more efficiently fulfill Python developer’s requirements.



NumPy Data Types

NumPy has several data types, which are data types with a single character, for example i for integers and u for unsigned integers.

Before we discuss NumPy datatypes, let’s review Python’s default data types:

  1. strings – Quote marks are utilized to display text data. For example, “mrexamples”.
  2. integer – Numbers that are displayed as integers. For example, 7, -7.
  3. float – A number that describes a real number. For example, 4.33, 78.346.
  4. boolean – A True or False value is utilized to describe it.
  5. complex – Describe complex numbers. For example, 4 + 72j or 3.24 + 6.11j.

Here is a list of NumPy’s data types and the characters that identify each type.

  • b – boolean.
  • c – complex float.
  • f – float.
  • i – integer.
  • u – unsigned integer.
  • m – timedelta.
  • M – datetime.
  • O – object.
  • S – string.
  • U – unicode string.
  • V – Memory block remains the same for other types ( void ).

Array Data Type Check

A NumPy array object has an attribute known as dtype that displays the numpy data type of the array.

Find out what data type an array object has:

Example: 1 

import numpy as npy even_arr = npy.array([0, 2, 4, 6, 8, 10]) print(even_arr.dtype)

Example: 2 

import numpy as npy odd_arr = npy.array([1, 3, 5, 7, 9, 11]) print(odd_arr.dtype)

An array having strings can be characterised by its data type:

Example: 3 

import numpy as npy sameWords_arr = npy.array(['bass', 'base', 'blew', 'blue', 'cell', 'sell', 'dear', 'deer']) print(sameWords_arr.dtype)

Example: 4 

import numpy as npy silent_arr = npy.array(["align", "bridge", "design", "edge", "anchor", "muscle", "echo", "doubt"]) print(silent_arr.dtype)

 


Defined Data Types For Arrays

For numpy data types, we utilize the array() function to generate arrays.

This function can accept an additional argument: dtype, which indicates the expected data type associated with the array items.

Arrays with string data types are generated as follows:

Example: 1 

import numpy as npy even_arr = npy.array([0, 2, 4, 6, 8, 10], dtype='S') print(even_arr) print(even_arr.dtype)

Example: 2 

import numpy as npy odd_arr = npy.array([1, 3, 5, 7, 9, 11], dtype='S') print(odd_arr) print(odd_arr.dtype)

We can also set sizes for i, u, f, S, and U.

Assign data types 2 bytes integer to an array:

Example: 3 

import numpy as npy even_arr = npy.array([0, 2, 4, 6, 8, 10], dtype='i2′) print(even_arr) print(even_arr.dtype)

Example: 4 

import numpy as npy odd_arr = npy.array([1, 3, 5, 7, 9, 11], dtype='i2′) print(odd_arr) print(odd_arr.dtype)

Can’t change a value?

When it comes to numpy data types, NumPy throws a Value Error if data fails to be cast.

ValueError: When a function receives an unexpected or invalid argument type, a ValueError is thrown.

It is not possible to transform a non-integer string like ‘mrexamples’ to an integer (will throw an exception):

Example: 

import numpy as npy mrx_arr = npy.array(['1', 'mrexamples', '*'], dtype='i')

Example: 

import numpy as npy mrx_arr = npy.array(['1', 'mrexamples', '*'], dtype='i') print(mrx_arr)

Example: 

import numpy as npy leagues_arr = npy.array(["Major League Soccer","Premier League","La Liga","Serie A","Bundesliga"], dtype='i') print(leagues_arr)

 


Existing Arrays Can Be Converted

As far as numpy data types are concerned, the best way to modify the type of an array is to copy it and execute the astype() method.

By passing the data type as a parameter, the astype() function makes a copy of the array.

Data types can be defined utilizing a string, such as ‘s‘ for string, ‘i‘ for integer, etc., or utilizing the data type directly, such as float for float and int for integer.

You can convert the data type from float to integer by setting the parameter value ‘i‘:

Example: 

import numpy as npy mrx_arr = npy.array([2.67, 7.4, 4.51, 28.678]) ample_arr = mrx_arr.astype('i') print(ample_arr) print(ample_arr.dtype)

Example: 

import numpy as npy mrx_arr = npy.array([-36.89, -874.22, -4.51, -7.123]) ample_arr = mrx_arr.astype('i') print(ample_arr) print(ample_arr.dtype)

By passing int as the parameter value, you can transform the data type from float to integer:

Example: 

import numpy as npy mrx_arr = npy.array([2.67, 7.4, 4.51, 28.678]) ample_arr = mrx_arr.astype(int) print(ample_arr) print(ample_arr.dtype)

Example: 

import numpy as npy mrx_arr = npy.array([-36.89, -874.22, -4.51, -7.123]) ample_arr = mrx_arr.astype(int) print(ample_arr) print(ample_arr.dtype)

To transform numpy data type from integer to boolean, follow the below examples:

Example: 

import numpy as npy pattern_arr = npy.array([0, 1, 0, 2, 0, 3]) mrx_arr = pattern_arr.astype(bool) print(mrx_arr) print(mrx_arr.dtype)

Example: 

import numpy as npy pattern_arr = npy.array([-0, -1, -0, -2, -0, -3]) mrx_arr = pattern_arr.astype(bool) print(mrx_arr) print(mrx_arr.dtype)
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *