Lesson 3 of 9 · 21m
Dtypes, Missing Values, and the Cost of Object Columns
Why dtype is a semantic contract, what object columns hide, and how nullable types change missing-data handling.
Dtypes are not implementation trivia. They are pandas' compressed statement about what a column means, how much memory it costs, which operations are valid, and what counts as missing. Most painful production bugs in tabular pipelines are type bugs wearing a data costume.
object is not a type — it is a junk drawer
df.dtypes
# user_id object
# created_at object
# amount objectAn object column means "Python objects in a pointer array." It might contain strings, dicts, None, numbers, or all four. Memory use is high, vectorized operations are impossible, and comparisons can be order-dependent. A CSV read often produces object columns because pandas could not prove anything stronger — not because the data is truly unstructured.
Convert deliberately:
df["created_at"] = pd.to_datetime(df["created_at"], utc=True)
df["amount"] = pd.to_numeric(df["amount"], errors="coerce")
df["country"] = df["country"].astype("category")errors="coerce" is useful only if bad values are acceptable as missing. If they are not, let it fail. A loud parse error in development is cheaper than a quiet NaN in finance reporting.
NaN, None, NA, NaT: missingness is fractured for historical reasons
NumPy float arrays use NaN. Datetimes use NaT. Python objects often use None. Modern pandas nullable dtypes — Int64, boolean, string — use a single sentinel: pd.NA. The capital-I nullable integer matters because plain int64 cannot represent missing values; one missing integer traditionally forced the entire column into float64.
df["retry_count"] = df["retry_count"].astype("Int64")
df["is_internal"] = df["is_internal"].astype("boolean")
df["email_domain"] = df["email_domain"].astype("string")This preserves integer semantics and gives predictable boolean logic. It is one of the few dtype migrations that almost always improves both correctness and memory.
Categoricals are compression plus vocabulary
category is ideal for repeated low-cardinality strings: country, plan tier, event type, severity. It is terrible for IDs. The category list also defines valid values, which can expose typos like "de" versus "DE" before aggregation splits them apart.
Field notes
df.dtypesis a required post-load audit, not an optional detail.- Never treat
objectas final unless the column truly contains heterogeneous Python objects. pd.NApropagates through nullable boolean logic more sensibly thannp.nan.- Timestamps need timezone intent immediately;
objectdatetimes are a future incident. - String IDs should stay strings even when every character is numeric — leading zeros are data.
Was this lesson helpful?