Tsallaka zuwa abun ciki
Justice Tech School

Changing Types

astype converts a column. to_numeric and to_datetime handle messy text.

Example
df = pd.DataFrame({"n": ["1", "2", "x"], "d": ["2026-01-05", "2026-02-10", "2026-03-15"]})
df["num"] = pd.to_numeric(df["n"], errors="coerce")
df["d"] = pd.to_datetime(df["d"])
print(df.dtypes.astype(str).tolist())
print(df["num"].tolist())

Result

['str', 'datetime64[us]', 'float64']
[1.0, 2.0, nan]

Exercise

Convert to number.

pd.(s)

Quiz Test

What does errors="coerce" do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.