PD-01-005-P · Primer — Understanding dtypes
The idea in one sentence
Every column in a DataFrame has a dtype — a single label like int64 or object that tells pandas what kind of thing lives in that column — and if that label is wrong, your math and your sorting quietly go wrong with it.
Why it exists
A pandas column is not a loose bag of Python values; under the hood it is one tightly packed array where every value shares the same storage type. That shared type is the dtype, and it is the reason pandas is fast: because the whole column is guaranteed to be, say, 64-bit integers, pandas can add a million of them in one sweep instead of checking "what is this thing?" a million times. The dtype exists to make that promise explicit and to decide how each operation behaves — + means addition on a numeric column but glue-two-strings-together on a text column. So the dtype is not decoration. It is the switch that chooses which meaning of +, <, .mean(), and sort_values() you actually get. Getting fluent at reading dtypes is how you stop being surprised by results that look almost right but are secretly nonsense.
How it works
When pandas builds a column it runs type inference: it peeks at the values and guesses the narrowest type that fits — all whole numbers become int64, add a decimal and it becomes float64, and anything it can't confidently make numeric (mixed values, or plain text) falls back to the catch-all object dtype, which usually means "Python strings." You inspect every column's guess at once with df.dtypes, and you change a column's type on purpose with .astype(...). The trap is that numbers arriving from files or scraped tables often come in as text, so the column looks numeric but is really object, and pandas will happily concatenate '120' + '95' into '12095' instead of adding them. The fix is to convert with .astype(int) (or .astype(float)) so the values become real numbers and + means arithmetic again. Watch the sum flip when we change nothing but the dtype:
import pandas as pd
df = pd.DataFrame({
"city": ["Paris", "Lyon", "Nice"],
"signups": ["120", "95", "47"], # numbers stored as TEXT
})
print(df.dtypes)
# city object
# signups object <- text, not numeric!
# dtype: object
print(df["signups"].sum()) # text + text = glue
# 1209547
df["signups"] = df["signups"].astype(int) # convert on purpose
print(df.dtypes)
# city object
# signups int64 <- now real numbers
# dtype: object
print(df["signups"].sum()) # now it adds
# 262
Same column, same values on screen — the only thing that changed was the dtype, and the answer went from 1209547 to the correct 262.
The one gotcha
object is not "no type" — it is a real, sticky dtype, and pandas will not warn you when a numeric-looking column is actually text. Because object columns accept anything, every operation still runs; it just runs with string meaning. sum() concatenates, sort_values() orders "100" before "9" (because "1" < "9" character by character), and max() returns the alphabetically-last string, not the largest number. There is no error, no red text — only a result that is subtly, confidently wrong, which is far more dangerous than a crash. And .astype(int) is strict: it raises if even one value can't be parsed (an empty string, a stray "N/A", a comma in "1,200"), so for messy real-world data reach for pd.to_numeric(col, errors="coerce"), which turns un-parseable entries into NaN instead of blowing up. So the discipline is simple: read df.dtypes before you trust any calculation, and convert with intent rather than assuming pandas already inferred what you meant.
See it — how a wrong dtype quietly breaks the sum, and astype fixes it
flowchart TD
A["signups column<br/>['120','95','47']"] --> B{"What is<br/>its dtype?"}
B -->|"object (text)"| C["sum() glues strings"]
C --> D["'1209547'<br/>(wrong, no error)"]
B -->|".astype(int)"| E["values become<br/>real int64"]
E --> F["sum() adds numbers"]
F --> G["262 (correct)"]
The one-line takeaway
Read df.dtypes first and convert with .astype() (or pd.to_numeric) — a numeric column that's secretly object gives you no error, just quietly wrong answers.
Where you'll meet it
You'll meet dtypes the moment your data comes from anywhere real: pd.read_csv reads a column full of numbers but marks it object because one row had a blank or a "$", and suddenly your totals are string soup. You'll meet it when a chart plots categories in a bizarre order — "1, 10, 11, 2" — and the culprit is a numeric axis that's actually text. You'll meet it in interviews the instant someone asks "why is this sum wrong?" and the answer is one glance at df.dtypes. Learning to check the dtype before you compute — and to reach for .astype() for clean data and pd.to_numeric(..., errors="coerce") for messy data — is one of the very first habits that separates code that runs from code that's right.
Named concepts: dtypes, object vs numeric, astype, type inference
