Sub topic 2
NumPy dtypes: fixed-width values in contiguous arrays
This chapter assumes you can create a DataFrame and select columns, and that you know Python's int, float, and str. NumPy experience helps but is not required.
Each column is one array with one dtype
A pandas column is backed by an array. For most numeric data, that array is a NumPy array. A NumPy array has a single dtype, which is a description of what each element is and how many bytes it uses.
Because every element has the same dtype, NumPy stores values back to back in one block of memory. Element i sits at a fixed offset from the start, so access is fast and no per-value bookkeeping is needed.
import pandas as pd
df = pd.DataFrame({
"a": [1, 2, 3],
"b": [1.5, 2.5, 3.5],
"c": ["x", "y", "z"],
})
print(df.dtypes)Column a is int64, column b is float64, and column c shows as object in pandas 2.x. The object dtype is covered in the next chapter.
Reading a dtype name
A dtype name combines a kind and a width in bits.
| dtype | Meaning | Bytes per value | Range or note |
|---|---|---|---|
| `int8` | signed integer | 1 | -128 to 127 |
| `int16` | signed integer | 2 | -32,768 to 32,767 |
| `int32` | signed integer | 4 | about ±2.1 billion |
| `int64` | signed integer | 8 | about ±9.2 × 10^18 |
| `uint8` | unsigned integer | 1 | 0 to 255 |
| `float32` | floating point | 4 | about 7 significant digits |
| `float64` | floating point | 8 | about 15 to 16 significant digits |
| `bool` | True or False | 1 | one byte per value, not one bit |
| `datetime64[ns]` | timestamp | 8 | nanosecond resolution |
The width is fixed. A value never grows or shrinks to fit its contents. The number 1 in an int64 column takes 8 bytes, the same as 9_000_000_000.
Memory is rows times width
For fixed-width dtypes, the memory a column needs is predictable: the number of rows multiplied by the bytes per value. A column of one million int64 values takes 8,000,000 bytes. The same values in int32 take 4,000,000.
import pandas as pd
s = pd.Series(range(1_000_000), dtype="int64")
print(s.dtype, s.itemsize, s.memory_usage(index=False))
s32 = s.astype("int32")
print(s32.dtype, s32.itemsize, s32.memory_usage(index=False))The itemsize attribute reports bytes per value. Multiplying it by the length gives the figure from memory_usage. This exact relationship holds only for fixed-width dtypes. Strings are different, as the next chapter shows.
Default dtypes and their consequences
pandas picks a dtype when it builds a column. Python integers become int64, Python floats become float64, and True/False become bool. The defaults are safe but not frugal. A column of ages from 0 to 120 does not need 8 bytes per value.
Choosing a narrower dtype saves memory but risks overflow. NumPy integers wrap around silently when a value exceeds the range:
import numpy as np
x = np.array([250], dtype="uint8")
print(x + np.uint8(10))This prints [4], because 260 does not fit in uint8 and wraps modulo 256. (Recent NumPy versions may emit an overflow warning for scalar operations, but array results still wrap.) Check the range of your data before you narrow a dtype.
Why contiguous matters
Contiguous, same-width storage has three practical effects:
- Operations such as
s.sum()ors * 2run in compiled loops over the whole array, without Python-level work per element. - Memory use is easy to predict from the dtype.
- A column cannot hold a mix of types. If you assign a value of another type, pandas must either convert the whole column or fall back to a more general dtype.
The last point is the reason missing values, mixed data, and strings need special handling. Later chapters return to it.
Exercise
A DataFrame has 2,000,000 rows and one column of dtype int64 holding values between 0 and 100. Roughly how many bytes does that column use, and how many would it use as uint8? Why is uint8 safe here?
Show answer
As int64 it uses 2,000,000 × 8 = 16,000,000 bytes. As uint8 it uses 2,000,000 × 1 = 2,000,000 bytes. It is safe because uint8 holds 0 to 255, which covers 0 to 100. Sums over the column could still overflow uint8, so be careful with aggregations done in that dtype.
Exercise
Why can a NumPy-backed pandas column not store the integer 5 and the string "five" together while staying int64?
Show answer
An int64 column is one contiguous array of 8-byte slots, and every slot must hold an int64. A string does not fit that layout, so pandas would have to change the whole column to a more general dtype such as object.