Sub topic 4
dtypes and converting types
This chapter assumes you can create a Series and a DataFrame and know what an Index is. Basic NumPy awareness helps but is not required.
What a dtype is
A dtype (data type) describes how pandas stores the values in a Series. Each Series has one dtype. A DataFrame can hold a different dtype in each column.
Common dtypes:
| dtype | Holds | Example |
|---|---|---|
| int64 | whole numbers | 10, 20 |
| float64 | decimals, and missing values in numeric data | 0.7, NaN |
| bool | True or False | True |
| object | usually Python strings, or mixed values | "Oslo" |
| string | text, with a dedicated string type | "Oslo" |
| datetime64[ns] | dates and times | 2026-12-01 |
| category | a fixed set of labels | "EU" |
The dtype decides which operations work and how much memory the data uses. Adding two int64 columns gives numbers. Adding two object columns of strings joins the text.
Inspecting dtypes
Use Series.dtype for one Series and DataFrame.dtypes for every column.
import pandas as pd
df = pd.DataFrame({
"city": ["Oslo", "Lima", "Pune"],
"pop_m": ["0.7", "10.7", "3.1"],
"year": [2020, 2020, 2020],
})
print(df.dtypes)Here pop_m holds text, not numbers, because the values are in quotes. Pandas reports it as object. This is a common result when a file contains a stray character such as a footnote mark or a thousands separator.
Converting with astype
astype returns a new Series with the requested dtype. It raises an error if a value cannot be converted.
import pandas as pd
s = pd.Series(["0.7", "10.7", "3.1"])
nums = s.astype("float64")
print(nums.dtype) # float64
print(nums.sum()) # 14.5The original s is unchanged. To keep the result, assign it back, for example df["pop_m"] = df["pop_m"].astype("float64").
Converting messy data with to_numeric
Real data often contains bad entries. pd.to_numeric with errors="coerce" turns anything it cannot parse into NaN instead of failing.
import pandas as pd
raw = pd.Series(["1", "2", "n/a", "4"])
clean = pd.to_numeric(raw, errors="coerce")
print(clean.tolist()) # [1.0, 2.0, nan, 4.0]
print(clean.dtype) # float64The result is float64 because NaN is a float value. A column of integers with one missing entry therefore becomes float64. Use pd.to_datetime for dates in the same way.
A conversion workflow
Common errors
- ValueError on astype. A value such as
"n/a"cannot become a number. Clean the value or useto_numeric. - Forgetting to assign.
df["x"].astype(int)does not changedf. Assign the result back. - Integers with missing values. Plain
int64cannot hold NaN, so the column turns into float64. - Object columns that look numeric. Arithmetic on text may join strings or raise
TypeErrorinstead of adding.
Exercise
A column price has dtype object and contains "5", "7", and "unknown". Write one line that converts it to numbers so that "unknown" becomes NaN, and say which dtype results.
Show answer
df["price"] = pd.to_numeric(df["price"], errors="coerce"). The dtype is float64, because NaN is a float value.