Creating a NumPy DataType

Learn via video courses
Topics Covered

Overview

Python supports a fundamental set of data types. Compared to standard Python, NumPy provides a greater variety of data types. The capabilities of NumPy are expanded by the wider variety of data types. For the elements in an array, there are more possibilities for specifying the data type.

Introduction

In Python, a variable's datatype describes the type of value it possesses and the kinds of mathematical, relational, and logical operations that can be performed on it without producing an error.

In this article, we will go over the different types of data types Python has and the different methods of creating datatypes.

How to Create Numpy Data Types?

There are two ways of creating datatypes in NumPy; dtype and astype()

Using dtype as a Constructor

dtype as a constructor is used to create similar and dissimilar data types. We can pass various things in the constructor, like character codes, tuples, datatypes, etc. When we use dtype as a constructor, it returns a datatype object.

Syntax

Parameters

  • object: This compulsory argument is used to represent the object that is to be converted to the data type.
  • align: This optional argument is used to add padding to the fields to enumerate structures in C. It takes in boolean values.
  • copy: This optional argument is used to create a copy of dtype object.

Examples

Output

Explanation

By using the float16 parameter in the dtype constructor, we have created a new datatype of float called "e".

Using dtype as an Argument

When we need to create a variable with a specific datatype only, we use dtype as an argument.

Syntax

Examples

Output

Using astype()

In Python, we can easily change the data type of an array using the astype() function. The dtype is an argument that the function accepts. All built-in and generic data types are supported by the function. The astype() function returns a series object with the converted datatype.

Syntax

Examples

Output

Different NumPy Data Types Arrays

NumPy offers us a lot more datatypes than Python, hence it's preferred over Python for computation purposes. In this section, we will go over some of the datatypes in NumPy arrays that are very popular among developers.

NumPy Numeric Data Types

Numeric datatypes in NumPy include the signed and unsigned integer values, floating-point numbers, and complex numbers. These datatypes are instances of dtype (datatype) objects that we covered earlier in this article.

NumPy Numeric DatatypeCharacter CodesMeaning
int8b, i18-bit signed integer ranging from -128 to 127
int16h, <i216-bit signed integer ranging from -32768 to 32767
uint8B, u18-bit unsigned integer ranging from 0 to 255
uint16H, u216-bit(2 bytes) unsigned integer ranging from 0 to 65535.

NumPy String Data Types

A string in NumPy is a collection of characters that are set in order. NumPy has two classifications when it comes to string datatypes; String and Unicode.

NumPy String DatatypeCharacter CodesMeaning
S or stringS(x) or |SxString datatype whose size is equal to x bytes.
U or unicodeU(x) or <UxUnicode datatype whose size is equal to 4*x bytes.

Output

NumPy Scalar Data Types

Any item you include in an array is referred to as a scalar in NumPy. It is related to the idea of a field element used to define a vector space in linear algebra. All scalars in an array must be of the same type, thanks to NumPy.

It is not feasible for one scalar to be of type int32 while the others are of type int64. NumPy is homogeneous, to put it another way.

DatatypeMeaning
bool_Return boolean true or false values.
int_Returns default integer type (int64 or int32)
float_Assigning float values

NumPy Datetime Data Type

In NumPy, we have a standard, and fast datatype system called the datetime64. We cannot use datetime, as it's already taken by the standard Python library.

Dates are represented using the current Gregorian Calendar and are infinitely extended in the past and future, just like the Python date class.

When we call the datetime64 function, it returns a new date with the specified format in the parameter.

Output

Conclusion

  • In this article, we understood what a datatype is, and different methods to create a datatype in NumPy.
  • To create datatypes, we learned three methods; dtype, dtype as contructor, and astype().
  • Apart from the generic datatypes, we covered the NumPy numeric, string, and scalar datatypes, along with their character codes and their specific meanings.
  • To conclude, we learned about the datetime datatype in NumPy called datetime64; it expresses the date given in the parameter in Gregorian Calendar format.