module documentation

numerictypes: Define the numeric type objects

This module is designed so "from numerictypes import *" is safe. Exported symbols include:

Dictionary with all registered number types (including aliases):
sctypeDict
Type objects (not all will be available, depends on platform):
see variable sctypes for which ones you have

Bit-width names

int8 int16 int32 int64 int128 uint8 uint16 uint32 uint64 uint128 float16 float32 float64 float96 float128 float256 complex32 complex64 complex128 complex192 complex256 complex512 datetime64 timedelta64

c-based names

bool_

object_

void, str_, unicode_

byte, ubyte, short, ushort intc, uintc, intp, uintp, int_, uint, longlong, ulonglong,

single, csingle, float_, complex_, longfloat, clongfloat,

As part of the type-hierarchy: xx -- is bit-width

generic
+-> bool_ (kind=b) +-> number | +-> integer | | +-> signedinteger (intxx) (kind=i) | | | byte | | | short | | | intc | | | intp int0 | | | int_ | | | longlong | | -> unsignedinteger (uintxx) (kind=u) | | ubyte | | ushort | | uintc | | uintp uint0 | | uint_ | | ulonglong | +-> inexact | +-> floating (floatxx) (kind=f) | | half | | single | | float_ (double) | | longfloat | -> complexfloating (complexxx) (kind=c) | csingle (singlecomplex) | complex_ (cfloat, cdouble) | clongfloat (longcomplex) +-> flexible | +-> character | | str_ (string_, bytes_) (kind=S) [Python 2] | | unicode_ (kind=U) [Python 2] | | | | bytes_ (string_) (kind=S) [Python 3] | | str_ (unicode_) (kind=U) [Python 3] | | | -> void (kind=V) -> object_ (not used much) (kind=O)
Variable cast Undocumented
Variable generic Undocumented
Variable generic​Type​Rank Undocumented
Variable nbytes Undocumented
Variable ​Scalar​Type Undocumented
Variable sctype​Dict Undocumented
Variable sctypes Undocumented
Variable typecodes Undocumented
Class ​_typedict Base object for a dictionary for look-up with any alias for an array dtype.
Function ​_can​_coerce​_all Undocumented
Function ​_construct​_lookups Undocumented
Function ​_find​_common​_coerce Undocumented
Function ​_register​_types Undocumented
Function ​_scalar​_type​_key A key function for sorted.
Function find​_common​_type Determine common type following standard coercion rules.
Function issctype Determines whether the given object represents a scalar data-type.
Function issubdtype Returns True if first argument is a typecode lower/equal in type hierarchy.
Function maximum​_sctype Return the scalar type of highest precision of the same kind as the input.
Function obj2sctype Return the scalar dtype or NumPy equivalent of Python type of an object.
Function sctype2char Return the string representation of a scalar dtype.
Variable __len​_test​_types Undocumented
Variable __test​_types Undocumented
Variable ​_alignment Undocumented
Variable ​_kind​_list Undocumented
Variable ​_maxvals Undocumented
Variable ​_minvals Undocumented
cast =

Undocumented

generic =

Undocumented

genericTypeRank: list[str] =

Undocumented

nbytes =

Undocumented

ScalarType =

Undocumented

sctypeDict: dict =

Undocumented

sctypes =

Undocumented

typecodes: dict[str, str] =

Undocumented

def _can_coerce_all(dtypelist, start=0):

Undocumented

def _construct_lookups():

Undocumented

def _find_common_coerce(a, b):

Undocumented

def _register_types():

Undocumented

def _scalar_type_key(typ):
A key function for sorted.
@set_module('numpy')
def find_common_type(array_types, scalar_types):

Determine common type following standard coercion rules.

Parameters

array_types : sequence
A list of dtypes or dtype convertible objects representing arrays.
scalar_types : sequence
A list of dtypes or dtype convertible objects representing scalars.

Returns

datatype : dtype
The common data type, which is the maximum of array_types ignoring scalar_types, unless the maximum of scalar_types is of a different kind (dtype.kind). If the kind is not understood, then None is returned.

See Also

dtype, common_type, can_cast, mintypecode

Examples

>>> np.find_common_type([], [np.int64, np.float32, complex])
dtype('complex128')
>>> np.find_common_type([np.int64, np.float32], [])
dtype('float64')

The standard casting rules ensure that a scalar cannot up-cast an array unless the scalar is of a fundamentally different kind of data (i.e. under a different hierarchy in the data type hierarchy) then the array:

>>> np.find_common_type([np.float32], [np.int64, np.float64])
dtype('float32')

Complex is of a different type, so it up-casts the float in the array_types argument:

>>> np.find_common_type([np.float32], [complex])
dtype('complex128')

Type specifier strings are convertible to dtypes and can therefore be used instead of dtypes:

>>> np.find_common_type(['f4', 'f4', 'i4'], ['c8'])
dtype('complex128')
@set_module('numpy')
def issctype(rep):

Determines whether the given object represents a scalar data-type.

Parameters

rep : any
If rep is an instance of a scalar dtype, True is returned. If not, False is returned.

Returns

out : bool
Boolean result of check whether rep is a scalar dtype.

See Also

issubsctype, issubdtype, obj2sctype, sctype2char

Examples

>>> np.issctype(np.int32)
True
>>> np.issctype(list)
False
>>> np.issctype(1.1)
False

Strings are also a scalar type:

>>> np.issctype(np.dtype('str'))
True
@set_module('numpy')
def issubdtype(arg1, arg2):

Returns True if first argument is a typecode lower/equal in type hierarchy.

This is like the builtin issubclass, but for dtypes.

Parameters

arg1, arg2 : dtype_like
dtype or object coercible to one

Returns

out : bool

See Also

:ref:`arrays.scalars` : Overview of the numpy type hierarchy. issubsctype, issubclass_

Examples

issubdtype can be used to check the type of arrays:

>>> ints = np.array([1, 2, 3], dtype=np.int32)
>>> np.issubdtype(ints.dtype, np.integer)
True
>>> np.issubdtype(ints.dtype, np.floating)
False
>>> floats = np.array([1, 2, 3], dtype=np.float32)
>>> np.issubdtype(floats.dtype, np.integer)
False
>>> np.issubdtype(floats.dtype, np.floating)
True

Similar types of different sizes are not subdtypes of each other:

>>> np.issubdtype(np.float64, np.float32)
False
>>> np.issubdtype(np.float32, np.float64)
False

but both are subtypes of floating:

>>> np.issubdtype(np.float64, np.floating)
True
>>> np.issubdtype(np.float32, np.floating)
True

For convenience, dtype-like objects are allowed too:

>>> np.issubdtype('S1', np.string_)
True
>>> np.issubdtype('i4', np.signedinteger)
True
@set_module('numpy')
def maximum_sctype(t):

Return the scalar type of highest precision of the same kind as the input.

Parameters

t : dtype or dtype specifier
The input data type. This can be a dtype object or an object that is convertible to a dtype.

Returns

out : dtype
The highest precision data type of the same kind (dtype.kind) as t.

See Also

obj2sctype, mintypecode, sctype2char dtype

Examples

>>> np.maximum_sctype(int)
<class 'numpy.int64'>
>>> np.maximum_sctype(np.uint8)
<class 'numpy.uint64'>
>>> np.maximum_sctype(complex)
<class 'numpy.complex256'> # may vary
>>> np.maximum_sctype(str)
<class 'numpy.str_'>
>>> np.maximum_sctype('i2')
<class 'numpy.int64'>
>>> np.maximum_sctype('f4')
<class 'numpy.float128'> # may vary
@set_module('numpy')
def obj2sctype(rep, default=None):

Return the scalar dtype or NumPy equivalent of Python type of an object.

Parameters

rep : any
The object of which the type is returned.
default : any, optional
If given, this is returned for objects whose types can not be determined. If not given, None is returned for those objects.

Returns

dtype : dtype or Python type
The data type of rep.

See Also

sctype2char, issctype, issubsctype, issubdtype, maximum_sctype

Examples

>>> np.obj2sctype(np.int32)
<class 'numpy.int32'>
>>> np.obj2sctype(np.array([1., 2.]))
<class 'numpy.float64'>
>>> np.obj2sctype(np.array([1.j]))
<class 'numpy.complex128'>
>>> np.obj2sctype(dict)
<class 'numpy.object_'>
>>> np.obj2sctype('string')
>>> np.obj2sctype(1, default=list)
<class 'list'>
@set_module('numpy')
def sctype2char(sctype):

Return the string representation of a scalar dtype.

Parameters

sctype : scalar dtype or object
If a scalar dtype, the corresponding string character is returned. If an object, sctype2char tries to infer its scalar type and then return the corresponding string character.

Returns

typechar : str
The string character corresponding to the scalar type.

Raises

ValueError
If sctype is an object for which the type can not be inferred.

See Also

obj2sctype, issctype, issubsctype, mintypecode

Examples

>>> for sctype in [np.int32, np.double, np.complex_, np.string_, np.ndarray]:
...     print(np.sctype2char(sctype))
l # may vary
d
D
S
O
>>> x = np.array([1., 2-1.j])
>>> np.sctype2char(x)
'D'
>>> np.sctype2char(list)
'O'
__len_test_types =

Undocumented

__test_types =

Undocumented

_alignment =

Undocumented

_kind_list: list[str] =

Undocumented

_maxvals =

Undocumented

_minvals =

Undocumented