Scalars¶
Python defines only one type of a particular data class (there is only one integer type, one floatingpoint 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 ndarrays
. 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
).
 1
However, array scalars are immutable, so none of the array scalar attributes are settable.
Builtin scalar types¶
The builtin scalar types are shown below. The Clike names are associated with character codes, which are shown in their descriptions. 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 type 
Related Python type 
Inherits? 

Python 2 only 

yes 

yes 

yes 

yes 

no 

no 

no 
The bool_
data type is very similar to the Python
bool
but does not inherit from it because Python’s
bool
does not allow itself to be inherited from, and
on the Clevel the size of the actual bool data is not the same as a
Python Boolean scalar.
Warning
The int_
type does not inherit from the
int
builtin under Python 3, because type int
is no
longer a fixedwidth integer type.
Tip
The default data type in NumPy is float_
.

class
numpy.
generic
[source]¶ Base class for numpy scalar types.
Class from which most (all?) numpy scalar types are derived. For consistency, exposes the same API as
ndarray
, despite many consequent attributes being either “getonly,” or completely irrelevant. This is the class from which it is strongly suggested users should derive custom scalar types.
Integer types¶
Signed integer types¶

class
numpy.
byte
[source]¶ Signed integer type, compatible with C
char
. Character code
'b'
 Alias on this platform
numpy.int8
: 8bit signed integer (128
to127
).

class
numpy.
short
[source]¶ Signed integer type, compatible with C
short
. Character code
'h'
 Alias on this platform
numpy.int16
: 16bit signed integer (32_768
to32_767
).

class
numpy.
intc
[source]¶ Signed integer type, compatible with C
int
. Character code
'i'
 Alias on this platform
numpy.int32
: 32bit signed integer (2_147_483_648
to2_147_483_647
).

class
numpy.
int_
[source]¶ Signed integer type, compatible with Python
int
and Clong
. Character code
'l'
 Alias on this platform
numpy.int64
: 64bit signed integer (9_223_372_036_854_775_808
to9_223_372_036_854_775_807
). Alias on this platform
numpy.intp
: Signed integer large enough to fit pointer, compatible with Cintptr_t
.
Unsigned integer types¶

class
numpy.
ubyte
[source]¶ Unsigned integer type, compatible with C
unsigned char
. Character code
'B'
 Alias on this platform
numpy.uint8
: 8bit unsigned integer (0
to255
).

class
numpy.
ushort
[source]¶ Unsigned integer type, compatible with C
unsigned short
. Character code
'H'
 Alias on this platform
numpy.uint16
: 16bit unsigned integer (0
to65_535
).

class
numpy.
uintc
[source]¶ Unsigned integer type, compatible with C
unsigned int
. Character code
'I'
 Alias on this platform
numpy.uint32
: 32bit unsigned integer (0
to4_294_967_295
).

class
numpy.
uint
[source]¶ Unsigned integer type, compatible with C
unsigned long
. Character code
'L'
 Alias on this platform
numpy.uint64
: 64bit unsigned integer (0
to18_446_744_073_709_551_615
). Alias on this platform
numpy.uintp
: Unsigned integer large enough to fit pointer, compatible with Cuintptr_t
.
Inexact types¶

class
numpy.
inexact
[source]¶ Abstract base class of all numeric scalar types with a (potentially) inexact representation of the values in its range, such as floatingpoint numbers.
Floatingpoint types¶

class
numpy.
half
[source]¶ Halfprecision floatingpoint number type.
 Character code
'e'
 Alias on this platform
numpy.float16
: 16bitprecision floatingpoint number type: sign bit, 5 bits exponent, 10 bits mantissa.

class
numpy.
single
[source]¶ Singleprecision floatingpoint number type, compatible with C
float
. Character code
'f'
 Alias on this platform
numpy.float32
: 32bitprecision floatingpoint number type: sign bit, 8 bits exponent, 23 bits mantissa.

class
numpy.
double
[source]¶ Doubleprecision floatingpoint number type, compatible with Python
float
and Cdouble
. Character code
'd'
 Alias
 Alias on this platform
numpy.float64
: 64bit precision floatingpoint number type: sign bit, 11 bits exponent, 52 bits mantissa.

class
numpy.
longdouble
[source]¶ Extendedprecision floatingpoint number type, compatible with C
long double
but not necessarily with IEEE 754 quadrupleprecision. Character code
'g'
 Alias
 Alias on this platform
numpy.float128
: 128bit extendedprecision floatingpoint number type.
Complex floatingpoint types¶

class
numpy.
complexfloating
[source]¶ Abstract base class of all complex number scalar types that are made up of floatingpoint numbers.

class
numpy.
csingle
[source]¶ Complex number type composed of two singleprecision floatingpoint numbers.
 Character code
'F'
 Alias
 Alias on this platform
numpy.complex64
: Complex number type composed of 2 32bitprecision floatingpoint numbers.

class
numpy.
cdouble
[source]¶ Complex number type composed of two doubleprecision floatingpoint numbers, compatible with Python
complex
. Character code
'D'
 Alias
 Alias
 Alias on this platform
numpy.complex128
: Complex number type composed of 2 64bitprecision floatingpoint numbers.

class
numpy.
clongdouble
[source]¶ Complex number type composed of two extendedprecision floatingpoint numbers.
 Character code
'G'
 Alias
 Alias
 Alias on this platform
numpy.complex256
: Complex number type composed of 2 128bit extendedprecision floatingpoint numbers.
Other types¶

class
numpy.
bool_
[source]¶ Boolean type (True or False), stored as a byte.
Warning
The
bool_
type is not a subclass of theint_
type (thebool_
is not even a number type). This is different than Python’s default implementation ofbool
as a subclass ofint
. Character code
'?'
 Alias

class
numpy.
datetime64
[source]¶ A datetime stored as a 64bit integer, counting from
19700101T00:00:00
.>>> np.datetime64(10, 'Y') numpy.datetime64('1980') >>> np.datetime64(10, 'D') numpy.datetime64('19700111')
See Datetimes and Timedeltas for more information.
 Character code
'M'

class
numpy.
timedelta64
[source]¶ A timedelta stored as a 64bit integer.
See Datetimes and Timedeltas for more information.
 Character code
'm'
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 lists
, 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.)

class
numpy.
flexible
[source]¶ Abstract base class of all scalar types without predefined length. The actual size of these types depends on the specific np.dtype instantiation.

class
numpy.
bytes_
[source]¶ A byte string.
When used in arrays, this type strips trailing null bytes.
 Character code
'S'
 Alias

class
numpy.
str_
[source]¶ A unicode string.
When used in arrays, this type strips trailing null codepoints.
Unlike the builtin
str
, this supports the Buffer Protocol, exposing its contents as UCS4:>>> m = memoryview(np.str_("abc")) >>> m.format '3w' >>> m.tobytes() b'a\x00\x00\x00b\x00\x00\x00c\x00\x00\x00'
 Character code
'U'
 Alias

class
numpy.
void
[source]¶ Either an opaque sequence of bytes, or a structure.
>>> np.void(b'abcd') void(b'\x61\x62\x63\x64')
Structured
void
scalars can only be constructed via extraction from Structured arrays:>>> arr = np.array((1, 2), dtype=[('x', np.int8), ('y', np.int8)]) >>> arr[()] (1, 2) # looks like a tuple, but is `np.void`
 Character code
'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
struct
module.
Sized aliases¶
Along with their (mostly)
Cderived names, the integer, float, and complex datatypes are also
available using a bitwidth convention so that an array of the right
size can always be ensured. Two aliases (numpy.intp
and numpy.uintp
)
pointing to the integer type that is sufficiently large to hold a C pointer
are also provided.

numpy.
bool8
[source]¶ alias of
numpy.bool_

numpy.
int8
[source]¶ 
numpy.
int16
¶ 
numpy.
int32
¶ 
numpy.
int64
¶ Aliases for the signed integer types (one of
numpy.byte
,numpy.short
,numpy.intc
,numpy.int_
andnumpy.longlong
) with the specified number of bits.Compatible with the C99
int8_t
,int16_t
,int32_t
, andint64_t
, respectively.

numpy.
uint8
[source]¶ 
numpy.
uint16
¶ 
numpy.
uint32
¶ 
numpy.
uint64
¶ Alias for the unsigned integer types (one of
numpy.byte
,numpy.short
,numpy.intc
,numpy.int_
andnumpy.longlong
) with the specified number of bits.Compatible with the C99
uint8_t
,uint16_t
,uint32_t
, anduint64_t
, respectively.

numpy.
intp
[source]¶ Alias for the signed integer type (one of
numpy.byte
,numpy.short
,numpy.intc
,numpy.int_
and np.longlong) that is the same size as a pointer.Compatible with the C
intptr_t
. Character code
'p'

numpy.
uintp
[source]¶ Alias for the unsigned integer type (one of
numpy.byte
,numpy.short
,numpy.intc
,numpy.int_
and np.longlong) that is the same size as a pointer.Compatible with the C
uintptr_t
. Character code
'P'

numpy.
float16
[source]¶ alias of
numpy.half

numpy.
float32
[source]¶ alias of
numpy.single

numpy.
float64
[source]¶ alias of
numpy.double

numpy.
float96
¶ 
numpy.
float128
[source]¶ Alias for
numpy.longdouble
, named after its size in bits. The existence of these aliases depends on the platform.

numpy.
complex64
[source]¶ alias of
numpy.csingle

numpy.
complex128
[source]¶ alias of
numpy.cdouble

numpy.
complex192
¶ 
numpy.
complex256
[source]¶ Alias for
numpy.clongdouble
, named after its size in bits. The existance of these aliases depends on the platform.
Other aliases¶
The first two of these are conveniences which resemble the names of the
builtin types, in the same style as bool_
, int_
, str_
, bytes_
, and
object_
:

numpy.
float_
[source]¶ alias of
numpy.double

numpy.
complex_
[source]¶ alias of
numpy.cdouble
Some more use alternate naming conventions for extendedprecision floats and complex numbers:

numpy.
longfloat
[source]¶ alias of
numpy.longdouble

numpy.
singlecomplex
[source]¶ alias of
numpy.csingle

numpy.
cfloat
[source]¶ alias of
numpy.cdouble

numpy.
longcomplex
[source]¶ alias of
numpy.clongdouble

numpy.
clongfloat
[source]¶ alias of
numpy.clongdouble
The following aliases originate from Python 2, and it is recommended that they not be used in new code.

numpy.
string_
[source]¶ alias of
numpy.bytes_

numpy.
unicode_
[source]¶ alias of
numpy.str_
Attributes¶
The array scalar objects have an array priority
of NPY_SCALAR_PRIORITY
(1,000,000.0). They also do not (yet) have a ctypes
attribute. Otherwise, they share the same attributes as arrays:
The integer value of flags. 

Tuple of array dimensions. 

Tuple of bytes steps in each dimension. 

The number of array dimensions. 

Pointer to start of data. 

The number of elements in the gentype. 

The length of one element in bytes. 

Scalar attribute identical to the corresponding array attribute. 

Get array datadescriptor. 

The real part of the scalar. 

The imaginary part of the scalar. 

A 1D view of the scalar. 

Scalar attribute identical to the corresponding array attribute. 

Array protocol: Python side 

Array protocol: struct 

Array priority. 

sc.__array_wrap__(obj) return scalar from array 
Indexing¶
See also
Array scalars can be indexed like 0dimensional arrays: if x is an array scalar,
x[()]
returns a copy of array scalarx[...]
returns a 0dimensionalndarray
x['fieldname']
returns the array scalar in the field fieldname. (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 0dimensional 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:
sc.__array__(dtype) return 0dim array from scalar with specified dtype 

sc.__array_wrap__(obj) return scalar from array 

Scalar method identical to the corresponding array attribute. 

Scalar method identical to the corresponding array attribute. 

Helper for pickle. 

Scalar method identical to the corresponding array attribute. 
Defining new types¶
There are two ways to effectively define a new array scalar type
(apart from composing structured types dtypes from
the builtin scalar types): One way is to simply subclass the
ndarray
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 datatype, and register it with NumPy. Such new types can only
be defined in C, using the NumPy CAPI.