Skip to content

NumPy dtypes Explained: Integer, Float, Boolean and String Types

NumPy dtype: types, sizes and ranges

Quick answer: A NumPy dtype describes how each element of an ndarray is stored and interpreted. Specify dtype=np.int32 for a fixed-width signed integer or dtype=np.float64 for double-precision floating point. Choose the type based on range, precision, memory and compatibility—not an assumed speedup.

For example, np.array([1, 2, 3], dtype=np.int32).dtype is int32. NumPy can infer a dtype when none is supplied, but the inferred type can vary with the data, platform and NumPy release.

NumPy numeric types at a glance

The existing reference table below lists common fixed-width numeric types. Integer limits are exact; floating-point values shown are approximate maximum finite magnitudes, not uniform-precision intervals. The C-type column is a representation analogy rather than a guarantee of identical C typedefs across platforms.

NumPy typeC typeDescription
np.int8<span class="pre">int8_t</span>8 bit signed Integer with range = -128 to 127
np.int16<span class="pre">int16_t</span>16 bit signed Integer with range = -32,768 to 32,767
np.int32<span class="pre">int32_t</span>32 bit signed Integer with range = -2.147483648E+9 to 2.147483647E+9
np.int64<span class="pre">int64_t</span>64 bit signed Integer with range = -9.223372036854775808E+18 to 9.223372036854775807E+18
np.uint8<span class="pre">uint8_t</span>8 bit unsigned Integer with range = 0 to 255
np.uint16<span class="pre">uint16_t</span>16 bit unsigned Integer with range = 0 to 65,535
np.uint32<span class="pre">uint32_t</span>32 bit unsigned Integer with range = 0 to 4.294967295E+9
np.uint64<span class="pre">uint64_t</span>64 bit unsigned Integer with range = 0 to 1.8446744073709551615E+19
np.intp<span class="pre">intptr_t</span>Integer used for indexing, typically the same as <span class="pre">ssize_t</span>
np.uintp<span class="pre">uintptr_t</span>Integer large enough to hold a pointer
np.float32<span class="pre">float</span>32-bit floating point; approximate largest finite magnitude 3.4028235 × 1038, with about 6–9 significant decimal digits.
np.float64 / legacy np.float_ (removed in NumPy 2.0)<span class="pre">double</span>64-bit IEEE 754 floating point (double precision); approximate largest finite magnitude 1.7976931348623157 × 10308. Precision is about 15–17 significant decimal digits.
np.complex64<span class="pre">float</span> <span class="pre">complex</span>Complex number, represented by two 32-bit floats (real and imaginary components)
np.complex128 / legacy np.complex_ (removed in NumPy 2.0)<span class="pre">double</span> <span class="pre">complex</span>Complex number, represented by two 64-bit floats (real and imaginary components).Note that this matches the precision of the builtin python complex.

Check dtype and item size in Python

Run these examples after installing NumPy (python -m pip install numpy). itemsize reports bytes per element, while nbytes reports the bytes used by array elements (not every allocation associated with the array).

import numpy as np

values = np.array([1, 2, 3], dtype=np.int32)
print(values.dtype)      # int32
print(values.itemsize)   # 4
print(values.nbytes)     # 12
print(np.iinfo(np.int8).min, np.iinfo(np.int8).max)  # -128 127
print(np.finfo(np.float32).max)  # approximately 3.4028235e+38

Choose a dtype without losing information

For integer data, use np.iinfo to verify the representable range. Unsigned integers cannot represent negative values. A cast to a narrower dtype can silently wrap or otherwise change values; check before conversion. For floating point, np.finfo reports machine epsilon, limits and precision. float32 uses less storage than float64, but it also has less precision; neither is automatically faster for every workload.

import numpy as np

data = np.array([100, 200, 250], dtype=np.int64)
limits = np.iinfo(np.uint8)
if np.any(data < limits.min) or np.any(data > limits.max):
    raise ValueError("Values will not fit in uint8")
compact = data.astype(np.uint8)
print(compact)  # [100 200 250]

Other dtype families and NumPy 2 compatibility

NumPy also supports Boolean (np.bool_), fixed-width Unicode strings (for example dtype='U10'), bytes strings, datetime/timedelta, structured dtypes and object references. The string width in U10 is a character count, not ten bytes. Newer NumPy releases also provide string facilities with version-specific availability. The aliases np.float_ and np.complex_ were removed in NumPy 2.0; prefer np.float64 and np.complex128.

Common errors and troubleshooting

  • Unexpected overflow: NumPy fixed-width integers differ from arbitrary-precision Python integers. Validate bounds before converting or multiplying.
  • Decimal rounding: binary floating point cannot represent many decimal fractions exactly; use tolerances for comparisons.
  • Unexpected string truncation: fixed-width string arrays can truncate longer values; choose an appropriate width or a suitable newer string dtype.
  • Deprecated alias errors: replace removed aliases with explicit fixed-width types.
  • Platform differences: inspect arr.dtype and arr.itemsize instead of assuming the width of the default integer type.

Frequently asked questions

What is the difference between dtype and type()? arr.dtype describes the stored element type; type(arr) identifies the Python class of the array object.

How can I find the range of an integer dtype? Use np.iinfo(np.int32). For floating-point characteristics, use np.finfo(np.float64).

Does a smaller dtype always make code faster? No. It reduces element storage, but speed depends on the algorithm, hardware, memory access and casting overhead.

See the official NumPy data types guide and dtype reference. For a broader introduction, read our Python arrays guide.

Leave a Reply

Your email address will not be published. Required fields are marked *