API reference
Data types
Supported: bool, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float16, float32, float64, complex64 and complex128. Each one is available as np.<name>, and anywhere a DTypeLike is accepted you can pass the name as a string. NumPy aliases float, double, single, half, int, complex and bool_ are also accepted.
| Name | Summary |
|---|---|
np.dtype | Resolves a name or DType to the canonical DType singleton, which has name, kind (b/i/u/f/c), itemSize and alignment. |
np.promoteTypes | The smallest dtype that both inputs can be safely cast to, using NumPy's promotion table. |
np.canCast | Whether from can be cast to to under a NumPy casting rule: "no", "equiv", "safe" (the default), "same_kind" or "unsafe". |
np.finfo | Machine limits of a float dtype (complex: of its component). |
np.iinfo | Limits of an integer dtype: bits, min, max, dtype, kind; min/max are numbers (lossy beyond 2^53) and minExact/maxExact exact bigints. |
np.resultType | The dtype NumPy's type promotion gives. |
np.minScalarType | The smallest dtype that holds the value of a JS scalar or a 0-d array (unsigned integers preferred for non-negative values; floats sized by NumPy's thresholds). |
np.issubdtype | Whether a is b (concrete dtype) or descends from the abstract dtype b (np.generic, np.number, np.integer, np.signedinteger, np.unsignedinteger, np.inexact, np.floating, np.complexfloating). |
np.isdtype | Array API dtype test. |
np.commonType | The float or complex dtype all inputs convert to (integers count as float64; bool raises DTypeError). |
np.mintypecode | NumPy's smallest-size type character from typeset among typechars (a string of type characters or a list of characters, DTypes or arrays). |
np.generic | Abstract dtype (NumPy's scalar type hierarchy, parent: null), for issubdtype. |
np.number | Abstract dtype (NumPy's scalar type hierarchy, parent: generic), for issubdtype. |
np.integer | Abstract dtype (NumPy's scalar type hierarchy, parent: number), for issubdtype. |
np.signedinteger | Abstract dtype (NumPy's scalar type hierarchy, parent: integer), for issubdtype. |
np.unsignedinteger | Abstract dtype (NumPy's scalar type hierarchy, parent: integer), for issubdtype. |
np.inexact | Abstract dtype (NumPy's scalar type hierarchy, parent: number), for issubdtype. |
np.floating | Abstract dtype (NumPy's scalar type hierarchy, parent: inexact), for issubdtype. |
np.complexfloating | Abstract dtype (NumPy's scalar type hierarchy, parent: inexact), for issubdtype. |
np.int_ | Default Python integer dtype: int64 on 64-bit platforms. |
np.intc | C int dtype: int32. |
np.intp | Pointer-sized signed integer dtype: int64 on 64-bit platforms. |
np.short | C short dtype: int16. |
np.byte | C signed char dtype: int8. |
np.ubyte | C unsigned char dtype: uint8. |
np.ushort | C unsigned short dtype: uint16. |
np.uint | Platform unsigned integer dtype: uint64 on 64-bit platforms. |
np.uintc | C unsigned int dtype: uint32. |
np.uintp | Pointer-sized unsigned integer dtype: uint64 on 64-bit platforms. |
np.ulong | C unsigned long dtype: uint64 on 64-bit platforms. |
np.double | Double-precision float dtype: float64. |
np.single | Single-precision float dtype: float32. |
np.half | Half-precision float dtype: float16. |
np.cdouble | Double-precision complex dtype: complex128. |
np.csingle | Single-precision complex dtype: complex64. |
np.longdouble | Long double dtype: aliased to float64 (no 80-bit float in JS). |
np.clongdouble | Long double complex dtype: aliased to complex128 (no 80-bit complex in JS). |
np.bytes_ | Byte-string dtype sentinel (string literal "bytes"). |
np.str_ | Unicode-string dtype sentinel (string literal "str"). |
np.datetime64 | Construct a datetime64 scalar from an ISO-8601 string, integer epoch offset, or another DatetimeArray. |
np.timedelta64 | Construct a timedelta64 scalar from an integer duration and unit. |
np.datetime_data | Returns the unit and count for a datetime64 or timedelta64 dtype. |
np.datetime_as_string | Convert a DatetimeArray to ISO-8601 string(s). |
np.dtype
#np.dtype(like)
Resolves a name or DType to the canonical DType singleton, which has name, kind (b/i/u/f/c), itemSize and alignment. Unknown names throw DTypeError.
Parameters
likeDTypeLike- Name, alias or DType.
Returns
DType
Example
const d = np.dtype("single");
d.name; // => "float32"
d.kind; // => "f"
d.itemSize; // => 4
d === np.float32; // => trueTypeScript declaration
np.dtype(like: DTypeLike): DTypenp.promoteTypes
#np.promoteTypes(a, b)
The smallest dtype that both inputs can be safely cast to, using NumPy's promotion table.
Parameters
a, bDTypeLike- Input dtypes.
Returns
DType
Example
np.promoteTypes("int32", "float32").name; // => "float64"
np.promoteTypes("uint8", "int8").name; // => "int16"TypeScript declaration
np.promoteTypes(a: DTypeLike, b: DTypeLike): DTypenp.canCast
#np.canCast(from, to, casting = "safe")
Whether from can be cast to to under a NumPy casting rule: "no", "equiv", "safe" (the default), "same_kind" or "unsafe". from can be a dtype or an array, in which case only its dtype is used. JS numbers raise DTypeError, as Python scalars do in NumPy 2. An unknown rule raises ValueError.
Parameters
fromDTypeLike | NDArray- Source dtype, or an array whose dtype is used.
toDTypeLike- Target dtype.
castingstring- Casting rule.
Returns
boolean
Example
np.canCast("int8", "int16"); // => true
np.canCast("float64", "float32"); // => false
np.canCast("float64", "float32", "same_kind"); // => true
np.canCast("int64", "uint8", "same_kind"); // => falseTypeScript declaration
np.canCast(from: DTypeLike | { readonly dtype: DType; }, to: DTypeLike, casting?: Casting | undefined): booleannp.finfo
#np.finfo(dtype | array)
Machine limits of a float dtype (complex: of its component). Fields as NumPy in camelCase: bits, eps, epsneg, max, min, tiny, smallestNormal, smallestSubnormal, resolution, precision, iexp, nexp, nmant, machep, negep, minexp, maxexp, dtype. Values are JS numbers holding the dtype's value. Non-float dtypes raise ValueError.
Returns
FInfo
Example
np.finfo("float16").max; // => 65504
np.finfo("float32").nmant; // => 23
np.finfo("float64").eps; // => 2.220446049250313e-16TypeScript declaration
np.finfo(dt: NDArray | DTypeLike): FInfonp.iinfo
#np.iinfo(dtype | array)
Limits of an integer dtype: bits, min, max, dtype, kind; min/max are numbers (lossy beyond 2^53) and minExact/maxExact exact bigints. Non-integer dtypes raise ValueError.
Returns
IInfo
Example
np.iinfo("int8").min; // => -128
np.iinfo("uint16").max; // => 65535
np.iinfo("uint64").maxExact === 18446744073709551615n; // => trueTypeScript declaration
np.iinfo(dt: NDArray | DTypeLike): IInfonp.resultType
#np.resultType(...arraysAndDtypes)
The dtype NumPy's type promotion gives. DTypes, dtype names and arrays are promoted with promoteTypes; JS scalars are weak (NEP 50): they can raise the kind (int → float → complex) but not the size.
Returns
DType
Example
np.resultType("int8", 300).name; // => "int8"
np.resultType("int8", 1.5).name; // => "float64"
np.resultType("float32", np.complex(0, 1)).name; // => "complex64"
np.resultType("int8", "uint8").name; // => "int16"TypeScript declaration
np.resultType(...args: (NDArray | NestedArray | DTypeLike)[]): DTypenp.minScalarType
#np.minScalarType(x)
The smallest dtype that holds the value of a JS scalar or a 0-d array (unsigned integers preferred for non-negative values; floats sized by NumPy's thresholds). Arrays with ndim > 0 return their dtype. JS numbers count as integers only when they are safe integers; use a bigint beyond 2^53.
Returns
DType
Example
np.minScalarType(10).name; // => "uint8"
np.minScalarType(-129).name; // => "int16"
np.minScalarType(3.1).name; // => "float16"
np.minScalarType(1e50).name; // => "float64"TypeScript declaration
np.minScalarType(a: NDArray | JSScalar): DTypenp.issubdtype
#np.issubdtype(a, b)
Whether a is b (concrete dtype) or descends from the abstract dtype b (np.generic, np.number, np.integer, np.signedinteger, np.unsignedinteger, np.inexact, np.floating, np.complexfloating).
Returns
boolean
Example
np.issubdtype("float32", np.floating); // => true
np.issubdtype("bool", np.integer); // => false
np.issubdtype("int8", "int16"); // => falseTypeScript declaration
np.issubdtype(a: DTypeLike | AbstractDType, b: DTypeLike | AbstractDType): booleannp.isdtype
#np.isdtype(dtype, kind)
Array API dtype test. kind is a DType, one of "bool", "signed integer", "unsigned integer", "integral", "real floating", "complex floating", "numeric", or an array of these. dtype must be a DType object.
Returns
boolean
Example
np.isdtype(np.float64, "real floating"); // => true
np.isdtype(np.int8, ["bool", "unsigned integer"]); // => false
np.isdtype(np.bool, "numeric"); // => falseTypeScript declaration
np.isdtype(dt: DType, kind: string | DType | readonly (string | DType)[]): booleannp.commonType
#np.commonType(...arrays)
The float or complex dtype all inputs convert to (integers count as float64; bool raises DTypeError). NumPy returns a scalar type; this returns a DType.
Returns
DType
Example
np.commonType(np.arange(3)).name; // => "float64"
np.commonType(np.zeros([1], { dtype: "float32" }), np.zeros([1], { dtype: "complex64" })).name; // => "complex64"TypeScript declaration
np.commonType(...arrays: (NDArray | NestedArray)[]): DTypenp.mintypecode
#np.mintypecode(typechars, typeset = "GDFgdf", default = "d")
NumPy's smallest-size type character from typeset among typechars (a string of type characters or a list of characters, DTypes or arrays). F with d gives D.
Returns
string
Example
np.mintypecode(["d", "f"]); // => "d"
np.mintypecode("dF"); // => "D"
np.mintypecode("i"); // => "d"TypeScript declaration
np.mintypecode(typechars: string | readonly (string | NDArray | NestedArray | DType)[], typeset?: string | undefined, defaultCode?: string | undefined): stringnp.generic
#np.generic
Abstract dtype (NumPy's scalar type hierarchy, parent: null), for issubdtype. Not usable as an array dtype.
Returns
AbstractDType
Example
np.issubdtype("float32", np.generic); // => truenp.number
#np.number
Abstract dtype (NumPy's scalar type hierarchy, parent: generic), for issubdtype. Not usable as an array dtype.
Returns
AbstractDType
Example
np.issubdtype("float32", np.number); // => truenp.integer
#np.integer
Abstract dtype (NumPy's scalar type hierarchy, parent: number), for issubdtype. Not usable as an array dtype.
Returns
AbstractDType
Example
np.issubdtype("float32", np.integer); // => falsenp.signedinteger
#np.signedinteger
Abstract dtype (NumPy's scalar type hierarchy, parent: integer), for issubdtype. Not usable as an array dtype.
Returns
AbstractDType
Example
np.issubdtype("float32", np.signedinteger); // => falsenp.unsignedinteger
#np.unsignedinteger
Abstract dtype (NumPy's scalar type hierarchy, parent: integer), for issubdtype. Not usable as an array dtype.
Returns
AbstractDType
Example
np.issubdtype("float32", np.unsignedinteger); // => falsenp.inexact
#np.inexact
Abstract dtype (NumPy's scalar type hierarchy, parent: number), for issubdtype. Not usable as an array dtype.
Returns
AbstractDType
Example
np.issubdtype("float32", np.inexact); // => truenp.floating
#np.floating
Abstract dtype (NumPy's scalar type hierarchy, parent: inexact), for issubdtype. Not usable as an array dtype.
Returns
AbstractDType
Example
np.issubdtype("float32", np.floating); // => truenp.complexfloating
#np.complexfloating
Abstract dtype (NumPy's scalar type hierarchy, parent: inexact), for issubdtype. Not usable as an array dtype.
Returns
AbstractDType
Example
np.issubdtype("float32", np.complexfloating); // => falsenp.int_
#np.int_
Default Python integer dtype: int64 on 64-bit platforms.
Returns
DType
Example
np.int_.name; // => "int64"TypeScript declaration
np.int_: DTypenp.intc
#np.intc
C int dtype: int32.
Returns
DType
Example
np.intc.name; // => "int32"TypeScript declaration
np.intc: DTypenp.intp
#np.intp
Pointer-sized signed integer dtype: int64 on 64-bit platforms.
Returns
DType
Example
np.intp.name; // => "int64"TypeScript declaration
np.intp: DTypenp.short
#np.short
C short dtype: int16.
Returns
DType
Example
np.short.name; // => "int16"TypeScript declaration
np.short: DTypenp.byte
#np.byte
C signed char dtype: int8.
Returns
DType
Example
np.byte.name; // => "int8"TypeScript declaration
np.byte: DTypenp.ubyte
#np.ubyte
C unsigned char dtype: uint8.
Returns
DType
Example
np.ubyte.name; // => "uint8"TypeScript declaration
np.ubyte: DTypenp.ushort
#np.ushort
C unsigned short dtype: uint16.
Returns
DType
Example
np.ushort.name; // => "uint16"TypeScript declaration
np.ushort: DTypenp.uint
#np.uint
Platform unsigned integer dtype: uint64 on 64-bit platforms.
Returns
DType
Example
np.uint.name; // => "uint64"TypeScript declaration
np.uint: DTypenp.uintc
#np.uintc
C unsigned int dtype: uint32.
Returns
DType
Example
np.uintc.name; // => "uint32"TypeScript declaration
np.uintc: DTypenp.uintp
#np.uintp
Pointer-sized unsigned integer dtype: uint64 on 64-bit platforms.
Returns
DType
Example
np.uintp.name; // => "uint64"TypeScript declaration
np.uintp: DTypenp.ulong
#np.ulong
C unsigned long dtype: uint64 on 64-bit platforms.
Returns
DType
Example
np.ulong.name; // => "uint64"TypeScript declaration
np.ulong: DTypenp.double
#np.double
Double-precision float dtype: float64.
Returns
DType
Example
np.double.name; // => "float64"TypeScript declaration
np.double: DTypenp.single
#np.single
Single-precision float dtype: float32.
Returns
DType
Example
np.single.name; // => "float32"TypeScript declaration
np.single: DTypenp.half
#np.half
Half-precision float dtype: float16.
Returns
DType
Example
np.half.name; // => "float16"TypeScript declaration
np.half: DTypenp.cdouble
#np.cdouble
Double-precision complex dtype: complex128.
Returns
DType
Example
np.cdouble.name; // => "complex128"TypeScript declaration
np.cdouble: DTypenp.csingle
#np.csingle
Single-precision complex dtype: complex64.
Returns
DType
Example
np.csingle.name; // => "complex64"TypeScript declaration
np.csingle: DTypenp.longdouble
#np.longdouble
Long double dtype: aliased to float64 (no 80-bit float in JS).
Returns
DType
Example
np.longdouble.name; // => "float64"np.clongdouble
#np.clongdouble
Long double complex dtype: aliased to complex128 (no 80-bit complex in JS).
Returns
DType
Example
np.clongdouble.name; // => "complex128"np.bytes_
#np.bytes_
Byte-string dtype sentinel (string literal "bytes"). In NumPy, bytes_ is the fixed-width byte-string dtype; in numera it is a string tag for dtype-aware APIs.
Returns
"bytes"
Example
np.bytes_; // => "bytes"TypeScript declaration
np.bytes_: "bytes"np.str_
#np.str_
Unicode-string dtype sentinel (string literal "str"). In NumPy, str_ is the fixed-width unicode dtype; in numera it is a string tag for dtype-aware APIs.
Returns
"str"
Example
np.str_; // => "str"TypeScript declaration
np.str_: "str"np.datetime64
#np.datetime64(value, [unit])
Construct a datetime64 scalar from an ISO-8601 string, integer epoch offset, or another DatetimeArray. The returned DatetimeArray holds int64 epoch offsets and a unit string.
Parameters
valuestring | number | bigint | DatetimeArray- ISO-8601 string (e.g.
"2023-01-15") or integer epoch offset. [unit]string- Datetime unit:
"Y""M""W""D""h""m""s""ms""us""ns". Inferred from the string when omitted.
Returns
DatetimeArray — a 0-d datetime64 array.
Example
np.datetime64("2023-01-15").unit; // => "D"TypeScript declaration
np.datetime64(value: DatetimeInput, unit?: string | undefined): DatetimeArraynp.timedelta64
#np.timedelta64(value, [unit])
Construct a timedelta64 scalar from an integer duration and unit.
Parameters
valuenumber | bigint | TimedeltaArray- Duration value.
[unit]string- Time unit (same set as
datetime64). Defaults to"s".
Returns
TimedeltaArray — a 0-d timedelta64 array.
Example
np.timedelta64(5, "D").unit; // => "D"TypeScript declaration
np.timedelta64(value: TimedeltaInput, unit?: string | undefined): TimedeltaArraynp.datetime_data
#np.datetime_data(dtype)
Returns the unit and count for a datetime64 or timedelta64 dtype. The count is always 1.
Parameters
dtypeDatetimeDType | TimedeltaDType | DatetimeArray | TimedeltaArray- A datetime or timedelta dtype or array.
Returns
[unit, count] — a tuple `[string, 1]`.
Example
np.datetime_data(np.datetime64("2023-01-15"))[0]; // => "D"np.datetime_as_string
#np.datetime_as_string(arr, [options])
Convert a DatetimeArray to ISO-8601 string(s). Optionally truncate to a coarser unit.
Parameters
arrDatetimeArray- The datetime array to format.
[options.unit]string- Output unit (truncates to this resolution). Defaults to the array's own unit.
[options.timezone]string- Timezone hint (accepted, not applied; all output is UTC).
Returns
string | string[] — one string per element.
Example
np.datetime_as_string(np.datetime64("2023-01-15")); // => "2023-01-15"TypeScript declaration
np.datetime_as_string(arr: DatetimeArray, options?: string | DatetimeAsStringOptions | undefined): string | string[]