Data types — NumPy v1.13 Manual (2024)

See also

Data type objects

Array types and conversions between types

NumPy supports a much greater variety of numerical types than Python does.This section shows which are available, and how to modify an array’s data-type.

Data typeDescription
bool_Boolean (True or False) stored as a byte
int_Default integer type (same as C long; normally eitherint64 or int32)
intcIdentical to C int (normally int32 or int64)
intpInteger used for indexing (same as C ssize_t; normallyeither int32 or int64)
int8Byte (-128 to 127)
int16Integer (-32768 to 32767)
int32Integer (-2147483648 to 2147483647)
int64Integer (-9223372036854775808 to 9223372036854775807)
uint8Unsigned integer (0 to 255)
uint16Unsigned integer (0 to 65535)
uint32Unsigned integer (0 to 4294967295)
uint64Unsigned integer (0 to 18446744073709551615)
float_Shorthand for float64.
float16Half precision float: sign bit, 5 bits exponent,10 bits mantissa
float32Single precision float: sign bit, 8 bits exponent,23 bits mantissa
float64Double precision float: sign bit, 11 bits exponent,52 bits mantissa
complex_Shorthand for complex128.
complex64Complex number, represented by two 32-bit floats (realand imaginary components)
complex128Complex number, represented by two 64-bit floats (realand imaginary components)

Additionally to intc the platform dependent C integer types short,long, longlong and their unsigned versions are defined.

NumPy numerical types are instances of dtype (data-type) objects, eachhaving unique characteristics. Once you have imported NumPy using

>>> import numpy as np

the dtypes are available as np.bool_, np.float32, etc.

Advanced types, not listed in the table above, are explored insection Structured arrays.

There are 5 basic numerical types representing booleans (bool), integers (int),unsigned integers (uint) floating point (float) and complex. Those with numbersin their name indicate the bitsize of the type (i.e. how many bits are neededto represent a single value in memory). Some types, such as int andintp, have differing bitsizes, dependent on the platforms (e.g. 32-bitvs. 64-bit machines). This should be taken into account when interfacingwith low-level code (such as C or Fortran) where the raw memory is addressed.

Data-types can be used as functions to convert python numbers to array scalars(see the array scalar section for an explanation), python sequences of numbersto arrays of that type, or as arguments to the dtype keyword that many numpyfunctions or methods accept. Some examples:

>>> import numpy as np>>> x = np.float32(1.0)>>> x1.0>>> y = np.int_([1,2,4])>>> yarray([1, 2, 4])>>> z = np.arange(3, dtype=np.uint8)>>> zarray([0, 1, 2], dtype=uint8)

Array types can also be referred to by character codes, mostly to retainbackward compatibility with older packages such as Numeric. Somedocumentation may still refer to these, for example:

>>> np.array([1, 2, 3], dtype='f')array([ 1., 2., 3.], dtype=float32)

We recommend using dtype objects instead.

To convert the type of an array, use the .astype() method (preferred) orthe type itself as a function. For example:

>>> z.astype(float) array([ 0., 1., 2.])>>> np.int8(z)array([0, 1, 2], dtype=int8)

Note that, above, we use the Python float object as a dtype. NumPy knowsthat int refers to np.int_, bool means np.bool_,that float is np.float_ and complex is np.complex_.The other data-types do not have Python equivalents.

To determine the type of an array, look at the dtype attribute:

>>> z.dtypedtype('uint8')

dtype objects also contain information about the type, such as its bit-widthand its byte-order. The data type can also be used indirectly to queryproperties of the type, such as whether it is an integer:

>>> d = np.dtype(int)>>> ddtype('int32')>>> np.issubdtype(d, int)True>>> np.issubdtype(d, float)False

Array Scalars

NumPy generally returns elements of arrays as array scalars (a scalarwith an associated dtype). Array scalars differ from Python scalars, butfor the most part they can be used interchangeably (the primaryexception is for versions of Python older than v2.x, where integer arrayscalars cannot act as indices for lists and tuples). There are someexceptions, such as when code requires very specific attributes of a scalaror when it checks specifically whether a value is a Python scalar. Generally,problems are easily fixed by explicitly converting array scalarsto Python scalars, using the corresponding Python type function(e.g., int, float, complex, str, unicode).

The primary advantage of using array scalars is thatthey preserve the array type (Python may not have a matching scalar typeavailable, e.g. int16). Therefore, the use of array scalars ensuresidentical behaviour between arrays and scalars, irrespective of whether thevalue is inside an array or not. NumPy scalars also have many of the samemethods arrays do.

Extended Precision

Python’s floating-point numbers are usually 64-bit floating-point numbers,nearly equivalent to np.float64. In some unusual situations it may beuseful to use floating-point numbers with more precision. Whether thisis possible in numpy depends on the hardware and on the developmentenvironment: specifically, x86 machines provide hardware floating-pointwith 80-bit precision, and while most C compilers provide this as theirlong double type, MSVC (standard for Windows builds) makeslong double identical to double (64 bits). NumPy makes thecompiler’s long double available as np.longdouble (andnp.clongdouble for the complex numbers). You can find out what yournumpy provides with``np.finfo(np.longdouble)``.

NumPy does not provide a dtype with more precision than Clong double``s; in particular, the 128-bit IEEE quad precisiondata type (FORTRAN's ``REAL*16) is not available.

For efficient memory alignment, np.longdouble is usually storedpadded with zero bits, either to 96 or 128 bits. Which is more efficientdepends on hardware and development environment; typically on 32-bitsystems they are padded to 96 bits, while on 64-bit systems they aretypically padded to 128 bits. np.longdouble is padded to the systemdefault; np.float96 and np.float128 are provided for users whowant specific padding. In spite of the names, np.float96 andnp.float128 provide only as much precision as np.longdouble,that is, 80 bits on most x86 machines and 64 bits in standardWindows builds.

Be warned that even if np.longdouble offers more precision thanpython float, it is easy to lose that extra precision, sincepython often forces values to pass through float. For example,the % formatting operator requires its arguments to be convertedto standard python types, and it is therefore impossible to preserveextended precision even if many decimal places are requested. It canbe useful to test your code with the value1 + np.finfo(np.longdouble).eps.

Data types — NumPy v1.13 Manual (2024)

FAQs

What are the datatypes in NumPy? ›

Types of data in NumPy
Data typeDescription
int16Integers ranging from -32 768 to 32 767
int32Integers ranging from -2 147 483 648 to 2 147 483 647
int64Integers ranging from -9 223 372 036 854 775 808 to 9 223 372 036 854 775 807
uint8Non-negative integers ranging from 0 to 255
6 more rows

What are the types of data in NumPy? ›

The two major types of numerical data are discrete and continuous. Discrete data is a type of numerical data which specific or fixed data values. Continuous data is data which lies within a given range of values. Operations can be performed on numerical data.

How to set data type in NumPy array? ›

The astype() function creates a copy of the array, and allows you to specify the data type as a parameter. The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float for float and int for integer.

What are the available types in NumPy? ›

There are 5 basic numerical types representing booleans ( bool ), integers ( int ), unsigned integers ( uint ) floating point ( float ) and complex .

How to check NumPy data type? ›

You can check the type of data present in the NumPy array using the '. dtype' attribute. The data type of array 'arr' is 'int' (integer). Here, '32' is related to memory allocation.

How to check data type in Python? ›

If you have a single parameter, the Python typeof function will return the type of that parameter. You can use this to check whether a parameter is a string, an integer, a list, etc. For example, if you have a variable x that is set to "Hello", you can use type (x) to check that it is a string.

What are the 4 main data types in Python? ›

The data types in Python that we will look at in this tutorial are integers, floats, Boolean, and strings. If you are not comfortable with using variables in Python, our article on how to declare python variables can change that.

What is the basic data structure in NumPy? ›

The main data structure in NumPy is the ndarray, which is a shorthand name for N-dimensional array. When working with NumPy, data in an ndarray is simply referred to as an array.

Can a NumPy array have different data types? ›

Structured arrays in NumPy allow you to create arrays with compound data types, where each element has multiple fields with different data types. The dtype parameter is used to specify the data type for each field.

What is the default datatype in NumPy? ›

The default data type: float64 . The 24 built-in array scalar type objects all convert to an associated data-type object.

How to convert list data type to NumPy array? ›

The simplest way to convert a Python list to a NumPy array is by using the `numpy. array()` function. This function takes a Python list as input and returns a NumPy array.

How to change data type in Python? ›

Type Conversion Functions in Python
  1. int(x): Converts the value x to an integer. ...
  2. float(x): Converts the value x to a float. ...
  3. str(x): Converts the value x to a string representation. ...
  4. bool(x): Converts the value x to a boolean (True or False).

What is the format of NumPy array? ›

The . npy format is the standard binary file format in NumPy for persisting a single arbitrary NumPy array on disk. The format stores all of the shape and dtype information necessary to reconstruct the array correctly even on another machine with a different architecture.

What is the difference between type and dtype in NumPy? ›

The type of a NumPy array is numpy. ndarray ; this is just the type of Python object it is (similar to how type("hello") is str for example). dtype just defines how bytes in memory will be interpreted by a scalar (i.e. a single number) or an array and the way in which the bytes will be treated (e.g. int / float ).

What does NumPy stand for? ›

NumPy stands for Numerical Python, and SciPy stands for Scientific Python; both are essential Python libraries. These libraries are used to manipulate data in various ways. In arrays of hom*ogeneous data, NumPy is used for efficient operations. SciPy is a set of Python tools.

What are data type in Python? ›

They determine the type of values variables can hold and specify the operations that can be performed on those values. For instance, Python has several built-in data types, including numeric types (int, float, complex), string (str), boolean (bool), and collection types (list, tuple, dict, set).

What are the 8 list datatypes available in numeric datatype? ›

The exact numeric types are INTEGER , BIGINT , DECIMAL , NUMERIC , NUMBER , and MONEY . Approximate numeric types, values where the precision needs to be preserved and the scale can be floating. The approximate numeric types are DOUBLE PRECISION , FLOAT , and REAL .

What are the data structures of NumPy? ›

The main data structure in NumPy is the ndarray, which is a shorthand name for N-dimensional array.

How many data structures are there in NumPy? ›

Numpy provides two data structures, the hom*ogeneous arrays and the structured (aka record) arrays.

Top Articles
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 6638

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.