What are the data types in NumPy?

Overview

NumPy, or simply numpy, is short for ‘Numerical Python’ and is a library that is used to deal with arrays.

An array in Python is simply a collection of multiple items of the same type. An array is also known as a list. The difference here is that a list takes longer to process in Python when compared to an array in numpy.

The numpy library also has functions that help manipulate numerical data, unlike Python, which is limited to the following data types by default:

  • integer: Represents integers (positive or negative whole numbers). For example, -5,-4, 2, 6, etc.
  • float: Represents floating-point numbers. For example, 0.1, 110.24, etc.
  • string: Represents text data given in quotation marks. For example, 'PYTHON`, “PYTHON”.
  • boolean- Represents statements, e.g., True or False.
  • complex- Represents complex numbers. For example, 2.0 + 3.5j, 1.2 + 3.5j, etc.

Numpy has all the data types listed above, and has even more data types than the traditional Python. These data types are always denoted with a character. Below is a list of some of the data types in Numpy that Python does not have:

  • unsigned integers(u): Represents a 32-bit non-negative integer (0 or positive numbers in the range of 23212^{32-1}.
  • timedelta(m): Calculates the duration between two dates and times.
  • datetime(M): Represents a single moment in time.
  • object(O): Represents how to interpret bytes in the fixed-sized block of memory that corresponds to an array item.
  • Unicode(U)- Represents Unicode strings.

How to get the data type of an array object in numpy

We use the dtype property in numpy to return the data type of an array.

Example

Let’s use the dtype property to check for the data types of some arrays.

import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
print('The array type for array1 is: ', array1.dtype)
array2 = np.array(['engineer', 'doctor', 'lawyer', 'Pilot'])
print('The array type for array2 is: ', array2.dtype)

Explanation

From the output of the program, int64 means that the array is an integer data type, while <U8 is a Unicode string, with the longest string being 8.

We can also create an array with a defined datatype using their respective codes.

Example

import numpy as np
# using the string datatype S
array1 = np.array([1, 2, 3, 4, 5], dtype = 'S')
print(array1)
print(array1.dtype)

Explanation

  • We import the numpy library.
  • We use the numpy.array() method to create an array and instruct it to create a string datatype with the S Unicode. We assign this to a variable we call array1
  • We print the array1 variable.
  • We use the dtype property to check for the data type.

Free Resources