Data types: dtype

Intermediate

Introduction

A Python list can hold anything: a number, a string, another list. A NumPy array cannot. Every element has the same dtype, and that is what lets NumPy store values side by side in memory and loop over them in compiled code. The price is that you must know which type your array has, because the type decides what values fit, how much memory the array takes, and what an operation gives back.

How it works

Every array has a dtype and an itemsize:

a = np.array([1, 2, 3])
print(a.dtype)       # int64
b = np.array([1, 2.5])
print(b)             # [1.  2.5]
print(b.dtype)       # float64

When you build an array from mixed values, NumPy picks a type that can hold them all: a single float makes the whole array float. The common types are bool, the signed integers int8, int16, int32 and int64 (the number is the bits), and the floats float32 and float64. On Linux and macOS, and on Windows since NumPy 2, the default integer is int64; older NumPy on Windows used int32.

Operations mixing types upcast: the result has a type that can represent both inputs. int8 + int64 gives int64; any integer with float64 gives float64; bool with anything gives the other type. One wrinkle: a small integer type with float32 stays float32, but int32 or int64 with float32 gives float64, because float32 cannot hold every large integer exactly.

astype converts, returning a new array. Floats to integers truncate toward zero: np.array([3.7, -1.2]).astype(int) gives array([ 3, -1]). Numbers to bool give False for zero and True otherwise.

Fixed-size integers do not grow like Python's int. An int8 holds -128 to 127, and a result outside that range overflows, wrapping around without any error:

a = np.array([100, 120], dtype=np.int8)
print(a + a)         # [-56 -16]

Floats have their own limit: 0.1 + 0.2 is 0.30000000000000004 in NumPy just as in Python, so compare floats with np.isclose, not ==.

Example

# A model of how NumPy picks the dtype of a + b, and of fixed-size integers wrapping around.
ORDER = ["bool", "int8", "int16", "int32", "int64", "float32", "float64"]

def result_type(a, b):
    if a.startswith("int") and b == "float32" or b.startswith("int") and a == "float32":
        other = a if a.startswith("int") else b
        return "float32" if other in ("int8", "int16") else "float64"
    return max(a, b, key=ORDER.index)  # otherwise the "larger" type wins

def wrap_int8(x):  # int8 holds -128 to 127; anything else wraps around
    return (x + 128) % 256 - 128

for a, b in [("int8", "int64"), ("int64", "float64"), ("bool", "int8"), ("int16", "float32"), ("int32", "float32")]:
    print(a, "+", b, "->", result_type(a, b))
print("int8: 100 + 100 =", wrap_int8(100 + 100))
print("int8: 120 + 120 =", wrap_int8(120 + 120))
print("int8: -100 - 100 =", wrap_int8(-100 - 100))
print("astype(int) of 3.7 and -1.2:", int(3.7), int(-1.2))
Open in compiler

Line by line

  1. ORDER ranks the types from narrowest to widest.
  2. result_type handles the float32 special case, then lets the wider type win.
  3. wrap_int8 models what happens to a result that does not fit in 8 bits.
  4. Python's int() truncates toward zero, exactly like astype(int).

Expected result

int8 + int64 -> int64
int64 + float64 -> float64
bool + int8 -> int8
int16 + float32 -> float32
int32 + float32 -> float64
int8: 100 + 100 = -56
int8: 120 + 120 = -16
int8: -100 - 100 = 56
astype(int) of 3.7 and -1.2: 3 -1

In the real world

Choosing dtypes saves memory: a million-pixel image as uint8 takes 1 MB, as float64 8 MB. Machine-learning code often uses float32 to halve memory and speed up GPUs. Overflow bugs are real: summing many int8 or int16 values, or squaring large int32 values, silently gives wrong answers, which is why counts and sums are kept in int64.

Common mistakes

  • Storing integers in a small type and then summing them, which overflows silently.
  • Expecting astype(int) to round: it truncates, so 2.9 becomes 2.
  • Comparing floats with == after arithmetic.
  • Assuming the default integer is the same everywhere: check a.dtype.

Best practices

  • Check dtype after loading data, before computing.
  • Use int64 and float64 unless memory forces something smaller.
  • Round explicitly with np.round before astype(int) when you mean rounding.

Summary

Every array has one dtype, chosen from its data or given with dtype=. Mixed operations upcast to a type that holds both inputs, astype converts with truncation toward zero, and fixed-size integers wrap around on overflow, so watch the types of anything you sum or multiply.

Quick check

1. What is the dtype of np.array([1, 2, 3.0])?
2. What does np.array([2.9, -2.9]).astype(int) give?
3. In int8, what is 100 + 100?
4. What is the dtype of an int32 array plus a float32 array?

Key takeaways

  • An array has one dtype; mixing types in one operation upcasts to a type that can hold both.
  • astype converts and copies: floats to ints truncate toward zero.
  • Fixed-size integers wrap around on overflow, silently, unlike Python's int.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.