The Problem
Real data moment
Real NOAA temperature readings — Central Park, January 2023, one real value per day — arrive in tenths of a degree Celsius, a unit nobody actually thinks in:
import numpy as np
raw_tenths = [128, 133, 144, 189, 100, 94, 72, 50, 67, 50, 39, 128, 139,
33, 50, 89, 83, 117, 67, 94, 56, 56, 39, 78, 122, 139, 67,
111, 117, 128, 100]
temps = np.array(raw_tenths)
celsius = temps / 10
print(celsius[:5])
print(round(celsius.mean(), 2))
[12.8 13.3 14.4 18.9 10. ]
9.29
One division, applied to all 31 real values simultaneously — no loop written anywhere. That's the entire practical reason NumPy exists, and it's worth understanding why it can do that.
The gotcha
Real station data occasionally has a data-entry quirk — a value recorded as text instead of a number. Build the same array with just one string slipped in:
messy = [128, 133, "144", 189, 100]
arr = np.array(messy)
print(arr.dtype)
print(arr)
<U21
['128' '133' '144' '189' '100']
Every single value — not just the one that was actually a string — silently became text (<U21: a Unicode string, up to 21 characters). No error, no warning at this point; the array was built successfully. The problem shows up one step later, the moment real math is attempted:
print(arr / 10)
TypeError: ufunc 'divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule 'safe'
A real TypeError — but notice it happens on the division line, not the array-creation line. Anyone debugging this by looking only at where the error appeared would miss the actual cause, which happened one full step earlier and silently.
Check .dtype the moment you build an array from real-world input
A Python list built from a file, an API, or user input can contain anything, including one unexpected string hiding among a thousand real numbers. print(arr.dtype) immediately after building an array from external data is a one-line habit that catches this before it becomes a confusing error several lines downstream.
np.array([12, 45, None, 78, 33]) is built from a list where one entry is None instead of a number. What's the actual resulting dtype?
Unlike the string case, NumPy doesn't automatically convert None into NaN — it upcasts to a plain object dtype and keeps the literal None sitting inside the array. Calling .mean() directly on it raises a real TypeError (Python can't add an int and a NoneType), because object arrays don't get NumPy's fast numeric math at all. Getting real NaN-aware behavior requires an explicit conversion first — .astype(float) turns that same None into a genuine nan, at which point np.nanmean() works correctly.