Scalars

Python defines only one type of a particular data class (there is only one integer type, one floating-point type, etc.). This can be convenient in applications that don’t need to be concerned with all the ways data can be represented in a computer. For scientific computing, however, more control is often needed.

In NumPy, there are 24 new fundamental Python types to describe different types of scalars. These type descriptors are mostly based on the types available in the C language that CPython is written in, with several additional types compatible with Python’s types.

Array scalars have the same attributes and methods as ndarraysopen in new window. [1] This allows one to treat items of an array partly on the same footing as arrays, smoothing out rough edges that result when mixing scalar and array operations.

Array scalars live in a hierarchy (see the Figure below) of data types. They can be detected using the hierarchy: For example, isinstance(val, np.generic) will return True if val is an array scalar object. Alternatively, what kind of array scalar is present can be determined using other members of the data type hierarchy. Thus, for example isinstance(val, np.complexfloating) will return True if val is a complex valued type, while isinstance(val, np.flexible) will return true if val is one of the flexible itemsize array types (string, unicode, void).

dtype-hierarchy

Figure: Hierarchy of type objects representing the array data types. Not shown are the two integer types intp and uintp which just point to the integer type that holds a pointer for the platform. All the number types can be obtained using bit-width names as well.

[1]However, array scalars are immutable, so none of the array scalar attributes are settable.

Built-in scalar types

The built-in scalar types are shown below. Along with their (mostly) C-derived names, the integer, float, and complex data-types are also available using a bit-width convention so that an array of the right size can always be ensured (e.g. int8, float64, complex128). Two aliases (intp and uintp) pointing to the integer type that is sufficiently large to hold a C pointer are also provided. The C-like names are associated with character codes, which are shown in the table. Use of the character codes, however, is discouraged.

Some of the scalar types are essentially equivalent to fundamental Python types and therefore inherit from them as well as from the generic array scalar type:

Array scalar typeRelated Python type
int_IntType (Python 2 only)
float_FloatType
complex_ComplexType
bytes_BytesType
unicode_UnicodeType

The bool_ data type is very similar to the Python BooleanType but does not inherit from it because Python’s BooleanType does not allow itself to be inherited from, and on the C-level the size of the actual bool data is not the same as a Python Boolean scalar.

Warning

The bool_ type is not a subclass of the int_ type (the bool_ is not even a number type). This is different than Python’s default implementation of boolopen in new window as a sub-class of int.

Warning

The int_ type does not inherit from the intopen in new window built-in under Python 3, because type intopen in new window is no longer a fixed-width integer type.

The default data type in NumPy is float_.

In the tables below, platform? means that the type may not be available on all platforms. Compatibility with different C or Python types is indicated: two types are compatible if their data is of the same size and interpreted in the same way.

Booleans:

TypeRemarksCharacter code
bool_compatible: Python bool'?'
bool88 bits

Integers:

TypeRemarksCharacter code
bytecompatible: C char'b'
shortcompatible: C short'h'
intccompatible: C int'i'
int_compatible: Python int'l'
longlongcompatible: C long long'q'
intplarge enough to fit a pointer'p'
int88 bits
int1616 bits
int3232 bits
int6464 bits

Unsigned integers:

TypeRemarksCharacter code
ubytecompatible: C unsigned char'B'
ushortcompatible: C unsigned short'H'
uintccompatible: C unsigned int'I'
uintcompatible: Python int'L'
ulonglongcompatible: C long long'Q'
uintplarge enough to fit a pointer'P'
uint88 bits
uint1616 bits
uint3232 bits
uint6464 bits

Floating-point numbers:

TypeRemarksCharacter code
half'e'
singlecompatible: C float'f'
doublecompatible: C double
float_compatible: Python float'd'
longfloatcompatible: C long float'g'
float1616 bits
float3232 bits
float6464 bits
float9696 bits, platform?
float128128 bits, platform?

Complex floating-point numbers:

TypeRemarksCharacter code
csingle'F'
complex_compatible: Python complex'D'
clongfloat'G'
complex64two 32-bit floats
complex128two 64-bit floats
complex192two 96-bit floats, platform?
complex256two 128-bit floats, platform?

Any Python object:

TypeRemarksCharacter code
object_any Python object'O'

Note

The data actually stored in object arrays (i.e., arrays having dtype object_) are references to Python objects, not the objects themselves. Hence, object arrays behave more like usual Python listsopen in new window, in the sense that their contents need not be of the same Python type.

The object type is also special because an array containing object_ items does not return an object_ object on item access, but instead returns the actual object that the array item refers to.

The following data types are flexible: they have no predefined size and the data they describe can be of different length in different arrays. (In the character codes # is an integer denoting how many elements the data type consists of.)

TypeRemarksCharacter code
bytes_compatible: Python bytes'S#'
unicode_compatible: Python unicode/str'U#'
void'V#'

Warning

See Note on string types.

Numeric Compatibility: If you used old typecode characters in your Numeric code (which was never recommended), you will need to change some of them to the new characters. In particular, the needed changes are c -> S1, b -> B, 1 -> b, s -> h, w -> H, and u -> I. These changes make the type character convention more consistent with other Python modules such as the structopen in new window module.

Attributes

The array scalar objects have an array priority of NPY_SCALAR_PRIORITY (-1,000,000.0). They also do not (yet) have a ctypesopen in new window attribute. Otherwise, they share the same attributes as arrays:

methoddescription
generic.flagsopen in new windowinteger value of flags
generic.shapeopen in new windowtuple of array dimensions
generic.stridesopen in new windowtuple of bytes steps in each dimension
generic.ndimopen in new windownumber of array dimensions
generic.dataopen in new windowpointer to start of data
generic.sizeopen in new windownumber of elements in the gentype
generic.itemsizeopen in new windowlength of one element in bytes
generic.baseopen in new windowbase object
generic.dtypeopen in new windowget array data-descriptor
generic.realopen in new windowreal part of scalar
generic.imagopen in new windowimaginary part of scalar
generic.flatopen in new windowa 1-d view of scalar
generic.Topen in new windowtranspose
generic.array_interfaceopen in new windowArray protocol: Python side
generic.array_structopen in new windowArray protocol: struct
generic.array_priorityopen in new windowArray priority.
generic.array_wrapopen in new window()sc.array_wrap(obj) return scalar from array

Indexing

Array scalars can be indexed like 0-dimensional arrays: if x is an array scalar,

  • x[()] returns a copy of array scalar
  • x[...] returns a 0-dimensional ndarrayopen in new window
  • x['field-name'] returns the array scalar in the field field-name. (x can have fields, for example, when it corresponds to a structured data type.)

Methods

Array scalars have exactly the same methods as arrays. The default behavior of these methods is to internally convert the scalar to an equivalent 0-dimensional array and to call the corresponding array method. In addition, math operations on array scalars are defined so that the same hardware flags are set and used to interpret the results as for ufunc, so that the error state used for ufuncs also carries over to the math on array scalars.

The exceptions to the above rules are given below:

methoddescription
genericopen in new windowBase class for numpy scalar types.
generic.arrayopen in new window()sc.array(dtype) return 0-dim array from scalar with specified dtype
generic.array_wrapopen in new window()sc.array_wrap(obj) return scalar from array
generic.squeezeopen in new window()Not implemented (virtual attribute)
generic.byteswapopen in new window()Not implemented (virtual attribute)
generic.reduceopen in new window()helper for pickle
generic.setstateopen in new window()
generic.setflagsopen in new window()Not implemented (virtual attribute)

Defining new types

There are two ways to effectively define a new array scalar type (apart from composing structured types dtypes from the built-in scalar types): One way is to simply subclass the ndarrayopen in new window and overwrite the methods of interest. This will work to a degree, but internally certain behaviors are fixed by the data type of the array. To fully customize the data type of an array you need to define a new data-type, and register it with NumPy. Such new types can only be defined in C, using the NumPy C-API.