Sub topic 2
Why numbers and dates end up as object
This chapter assumes you can build a DataFrame, index it, and load a CSV with pd.read_csv. It explains why a column that looks numeric or date-like is stored as text, so you know what to fix before you convert anything.
What object means
pandas gives each column one dtype. Numeric dtypes such as int64 and float64 store raw machine numbers in a compact array. The object dtype is the fallback: the column holds references to arbitrary Python objects, and each cell can be a different type. Depending on your pandas version, a column of pure text may show as object or as string. In both cases the values are text.
A column that should be numeric or a date becomes text when pandas cannot be sure every value fits a narrower dtype.
The usual causes
- Stray characters. A value like
"$1,200"is not a number to pandas. One such value is enough to make the whole column text. - Placeholders. Strings such as
"n/a"or"-"stand in for missing data, but pandas reads them as ordinary text unless told otherwise. - Mixed content. A column with integers, strings and
Nonehas no single narrower dtype that fits. - No automatic date parsing.
read_csvleaves date-like columns as text unless you ask for parsing. A single malformed date also keeps the column as text. - Leading zeros. Identifiers such as
"02134"look numeric, but they are labels, and converting them would lose information.
Seeing it in a small frame
The frame below has three columns, and all of them load as text.
import pandas as pd
df = pd.DataFrame({
"price": ["$1,200", "$85", "n/a"],
"qty": ["3", "5", "7"],
"date": ["2024-01-05", "2024-02-10", "bad"],
})
print(df.dtypes)
df.info()qty contains only digits, yet it is still text because the cells are strings. price and date each contain a value that could never parse. Seeing object in the output tells you the column's contents, not its meaning.
Mixed types inside one column
The object dtype can hide a mix of types. Check what is actually inside with map(type).
import pandas as pd
s = pd.Series([1, "2", 3.5, None])
print(s.dtype)
print(s.map(type))The column reports one dtype, but the cells are an int, a str, a float and a NoneType. Code that works on one row may fail on the next.
What it costs you
Text columns break operations that depend on numeric or date meaning. Sorting is the clearest example, because text sorts character by character.
import pandas as pd
as_text = pd.Series(["10", "9", "100"])
as_number = pd.Series([10, 9, 100])
print(as_text.sort_values().tolist())
print(as_number.sort_values().tolist())The text version gives ['10', '100', '9'], and the numeric version gives [9, 10, 100]. Other operations fail in a similar way:
- Arithmetic such as sums and means either raises an error or concatenates strings, depending on the version.
- Date features such as
.dt.monthare unavailable on text. - Memory use is higher, because each object cell is a separate Python object.
Reading the signs
When a column you expected to be numeric shows object or string, look for the cause before converting. Ask whether it contains symbols, placeholders, mixed types, or identifiers that should stay as text. The answer decides the fix: cleaning the strings, parsing with an error policy, or leaving the column alone. The next chapters cover each of these.
Exercise
A CSV column zip contains values like 02134 and 10001, and pandas loads it as text. Should you convert it to integers? Why or why not?
Show answer
No. A zip code is an identifier, not a quantity. Converting to integers would turn 02134 into 2134 and lose the leading zero. Keep it as text (for example the string dtype).
Exercise
A column holds the values "5", "12", "n/a". Give two reasons it is stored as text.
Show answer
The column contains a non-numeric placeholder ("n/a"), so pandas cannot store it as a numeric dtype. Also, even "5" and "12" are strings in the file, and pandas does not convert text to numbers unless it parses cleanly for the whole column.