Sub topic 13
Data types and overflow in aggregations
This chapter assumes you know how axis selects the direction of a reduction, and that every NumPy array has a fixed data type (dtype). Here you will see which dtype an aggregation uses for its result, and when it silently gives a wrong number.
Which dtype does the result have?
An aggregation adds values in an accumulator, the variable that holds the running result. The accumulator's dtype limits how large a result can be. NumPy does not always use the input's dtype for it.
| Input dtype | `sum` accumulates in |
|---|---|
| `bool` | `int64` (counts the `True` values) |
| `int8`, `int16`, `int32` | `int64` |
| `uint8`, `uint16`, `uint32` | `uint64` |
| `int64` | `int64` |
| `float32` | `float32` |
| `float64` | `float64` |
Small integer types are promoted to the platform's default integer. That is int64 on most systems. On Windows with NumPy older than 2.0 it is int32. The same rule applies to cumsum.
import numpy as np
a = np.full((2, 3), 100, dtype=np.int8)
print(a.sum(axis=1)) # [300 300]
print(a.sum(axis=1).dtype) # int64 on most platformsAn int8 can only hold values up to 127, but the row totals are 300. They are correct because the sum was promoted before adding.
Integer overflow wraps silently
Overflow happens when a result does not fit in its dtype. NumPy integers do not raise an error. They wrap around, like an odometer that rolls over.
You can trigger this by asking for a small accumulator with the dtype argument:
import numpy as np
a = np.full((2, 3), 100, dtype=np.int8)
print(a.sum(axis=1, dtype=np.int8)) # [44 44]The true total is 300. An int8 stores it as 300 − 256 = 44. No warning appears.
Promotion does not protect you once the values are already int64:
import numpy as np
big = np.array([2**62, 2**62], dtype=np.int64)
print(big.sum()) # -9223372036854775808The true sum is 2**63, one more than the largest int64, so it wraps to a large negative number.
Floats lose range or precision instead
Floating-point types do not wrap. Instead they overflow to inf, or they round away small contributions. A float16 holds values only up to 65504:
import numpy as np
h = np.array([60000, 60000], dtype=np.float16)
print(h.sum()) # inf, with a RuntimeWarning about overflowSumming many float32 values accumulates rounding error. A wider accumulator reduces it.
Choosing the accumulator with dtype
Every reduction accepts a dtype argument that sets the accumulator type. Pass a wider type than the input when totals might be large:
import numpy as np
h = np.array([60000, 60000], dtype=np.float16)
print(h.sum(dtype=np.float64)) # 120000.0
pixels = np.full((2, 3), 200, dtype=np.uint8)
print(pixels.sum(axis=0)) # [400 400 400]
print(pixels.mean(axis=0)) # [200. 200. 200.]mean on integers already uses float64 internally, so averages of uint8 data are safe. Only the sum-like functions (sum, cumsum, prod, cumprod) need your attention.
The dtype=object option stores exact Python integers and has no limit, but it is much slower. float64 is fast but approximate for integers above 2**53.
A quick check
Print result.dtype and compare the result with a rough estimate, such as the number of elements times a typical value. A mismatch usually points to a dtype problem.
Exercise
What does np.full((2, 3), 200, dtype=np.uint8).sum(axis=0, dtype=np.uint8) return, and why?
Show answer
It returns [144 144 144]. Each column adds 200 + 200 = 400, which does not fit in a uint8 (maximum 255). The value wraps to 400 − 256 = 144. Without dtype=np.uint8 the sum is promoted to uint64 and gives [400 400 400].