bundles / numpy latest / docs
Doc
Data type API
docs/reference:c-api:dtype
The standard array can have 25 different data types (and has some support for adding your own types). These data types all have an enumerated type, an enumerated type-character, and a corresponding array scalar Python type object (placed in a hierarchy). There are also standard C typedefs to make it easier to manipulate elements of the given data type. For the numeric types, there are also bit-width equivalent C typedefs and named typenumbers that make it easier to select the precision desired.
Enumerated types
Other useful related constants are
The various character codes indicating certain types are also part of an enumerated list. References to type characters (should they be needed at all) should always use these enumerations. The form of them is NPY_{NAME}LTR where {NAME} can be
BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, CFLOAT, CDOUBLE, CLONGDOUBLE, DATETIME, TIMEDELTA, OBJECT, STRING, UNICODE, VSTRING, VOID
INTP, UINTP
GENBOOL, SIGNED, UNSIGNED, FLOATING, COMPLEX
The latter group of {NAME}s corresponds to letters used in the array interface typestring specification.
Defines
Max and min values for integers
NPY_MAX_INT{bits},NPY_MAX_UINT{bits},NPY_MIN_INT{bits}These are defined for
{bits}= 8, 16, 32, 64, 128, and 256 and provide the maximum (minimum) value of the corresponding (unsigned) integer type. Note: the actual integer type may not be available on all platforms (i.e. 128-bit and 256-bit integers are rare).NPY_MIN_{type}This is defined for
{type}= BYTE, SHORT, INT, LONG, LONGLONG, INTPNPY_MAX_{type}This is defined for all defined for
{type}= BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, LONGLONG, ULONGLONG, INTP, UINTP
Number of bits in data types
All NPY_SIZEOF_{CTYPE} constants have corresponding NPY_BITSOF_{CTYPE} constants defined. The NPY_BITSOF_{CTYPE} constants provide the number of bits in the data type. Specifically, the available {CTYPE}s are
BOOL, CHAR, SHORT, INT, LONG, LONGLONG, FLOAT, DOUBLE, LONGDOUBLE
Bit-width references to enumerated typenums
All of the numeric data types (integer, floating point, and complex) have constants that are defined to be a specific enumerated type number. Exactly which enumerated type a bit-width type refers to is platform dependent. In particular, the constants available are PyArray_{NAME}{BITS} where {NAME} is INT, UINT, FLOAT, COMPLEX and {BITS} can be 8, 16, 32, 64, 80, 96, 128, 160, 192, 256, and 512. Obviously not all bit-widths are available on all platforms for all the kinds of numeric types. Commonly 8-, 16-, 32-, 64-bit integers; 32-, 64-bit floats; and 64-, 128-bit complex types are available.
Further integer aliases
The constants NPY_INTP and NPY_UINTP refer to an Py_ssize_t and size_t. Although in practice normally true, these types are strictly speaking not pointer sized and the character codes 'p' and 'P' can be used for pointer sized integers. (Before NumPy 2, intp was pointer size, but this almost never matched the actual use, which is the reason for the name.)
Since NumPy 2, NPY_DEFAULT_INT is additionally defined. The value of the macro is runtime dependent: Since NumPy 2, it maps to NPY_INTP while on earlier versions it maps to NPY_LONG.
C-type names
There are standard variable types for each of the numeric data types and the bool data type. Some of these are already available in the C-specification. You can create variables in extension code with these types.
Boolean
(Un)Signed Integer
Unsigned versions of the integers can be defined by prepending a 'u' to the front of the integer name.
(Complex) Floating point
complex types are structures with .real and .imag members (in that order).
Bit-width names
There are also typedefs for signed integers, unsigned integers, floating point, and complex floating point types of specific bit- widths. The available type names are
npy_int{bits},npy_uint{bits},npy_float{bits}, andnpy_complex{bits}
where {bits} is the number of bits in the type and can be 8, 16, 32, 64, 128, and 256 for integer types; 16, 32 , 64, 80, 96, 128, and 256 for floating-point types; and 32, 64, 128, 160, 192, and 512 for complex-valued types. Which bit-widths are available is platform dependent. The bolded bit-widths are usually available on all platforms.
Time and timedelta
Printf formatting
For help in printing, the following strings are defined as the correct format specifier in printf and related commands.