NumPy Data Types

Data types in Python

By default, Python has the following data types:

  • strings - Used to represent text data, which is enclosed in quotes. For example "ABCD".
  • integer - Used to represent integers. For example -1, -2, -3.
  • float - Used to represent real numbers. For example 1.2, 42.42.
  • boolean - Used to represent True or False.
  • complex - Used to represent numbers in the complex plane. For example 1.0 + 2.0j, 1.5 + 2.5j.

Data types in NumPy

NumPy has some additional data types, and data types are referenced by a character, for example i Representing integers,u Representing unsigned integers, etc.

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

  • i - Integer
  • b - Boolean
  • u - Unsigned integer
  • f - Floating-point
  • c - Complex floating-point number
  • m - Timedelta
  • M - Datetime
  • O - Object
  • S - String
  • U - Unicode string
  • V - Fixed memory block of other types (void)

Check the data type of the array

NumPy array objects have a named dtype The attribute, which returns the data type of the array:

Example

Get the data type of the array object:

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

Run Instance

Example

Get the data type of an array containing strings:

import numpy as np
arr = np.array(['apple', 'banana', 'cherry'])
print(arr.dtype)

Run Instance

to create an array with a predefined data type

We use array() The function to create an array, which can use optional parameters:dtypeIt allows us to define the expected data type of array elements:

Example

Create an array using a data type string:

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

Run Instance

For i,u,f,S and UWe can also define the size.

Example

Create an array with data type of 4-byte integer:

import numpy as np
arr = np.array([1, 2, 3, 4], dtype='i4')
print(arr)
print(arr.dtype)

Run Instance

What if the value cannot be converted?

If the type of the element cannot be forced to convert, NumPy will raise a ValueError.

ValueError: In Python, if the type of the parameter passed to the function is not expected or incorrect, ValueError will be raised.

Example

ValueError: In Python, if the type of the parameter passed to the function is unexpected or incorrect, ValueError will be raised.

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

Run Instance

Convert the data type of an existing array

The best way to change the data type of an existing array is to use astype() method copies the array.

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

Data types can be specified as strings, for example 'f' It represents a float,'i' You can also use data types directly, for example float It represents a float,int It represents an integer.

Example

By using 'i' Change the data type from float to integer as a parameter value:

import numpy as np
arr = np.array([1.1, 2.1, 3.1])
newarr = arr.astype('i')
print(newarr)
print(newarr.dtype)

Run Instance

Example

By using int Change the data type from float to integer as a parameter value:

import numpy as np
arr = np.array([1.1, 2.1, 3.1])
newarr = arr.astype(int)
print(newarr)
print(newarr.dtype)

Run Instance

Example

Change the data type from integer to boolean:

import numpy as np
arr = np.array([1, 0, 3])
newarr = arr.astype(bool)
print(newarr)
print(newarr.dtype)

Run Instance