Stop 08 · Data Cleaning & WranglingTopics · 4/9

Data Type Conversion

Part of the Data Analyst Roadmap

In short

Your totals are wrong, your sort says '100' comes before '20', and the date filter is greyed out. Nine times out of ten, a column has the wrong data type.

Fixing columns that were imported as the wrong type — dates read as text, numbers read as strings with commas — a small but extremely common source of broken calculations.

The jaadu pours water into an ice tray and holds up a tray of cubes.

Think of it like this

Water into ice cubes

Water in a jug can't go into a lunchbox. Pour it into a tray, freeze it, and the same water becomes cubes you can stack and count. Type conversion is pouring data into the right mould, text into numbers or dates, so tools can work with it.

Watch it happen

Converting a messy amount column

₹1,200
0
850
1
N/A
2
2,450.50
3
99
4

1/4Read from a CSV, this column is text (object). Sorting and summing won't behave.

The big ideas

What you need to know

Numbers

pd.to_numeric(col, errors="coerce") converts, and turns anything unreadable into NaN so you can find it.

Dates

pd.to_datetime with format= or dayfirst=True. 03/04/2026 is 3 April in India but March 4 in the US.

IDs stay text

Phone numbers, pincodes and account numbers are labels, not quantities. Keep them as strings.

Category type

Columns with few repeated values, like state, can be category type, saving memory on big data.

See it in code

Walk through the code

python
1df["amount"] = pd.to_numeric(2    df["amount"].astype(str).str.replace(r"[₹,\s]", "", regex=True),3    errors="coerce",4)5 6df["order_date"] = pd.to_datetime(df["order_date"], format="%d/%m/%Y", errors="coerce")7 8df["pincode"] = df["pincode"].astype(str).str.zfill(6)9df["state"] = df["state"].astype("category")10 11print(df.dtypes)12print("bad amounts:", df["amount"].isna().sum())

1/4A regex removes ₹, commas and whitespace, then unreadable values become NaN instead of crashing.

Convert money to numbers, Indian-style dates to datetimes, and keep pincodes as six-character text.

Save yourself the pain

Common mistakes

Oops

Using errors="coerce" and never checking how many values became NaN.

Do this instead

Count the new NaNs and look at a few. They might be a pattern you need to handle.

Oops

Letting Pandas guess date formats on Indian data.

Do this instead

Pass format= or dayfirst=True, then spot-check a date like 03/04 to confirm it's April.

Your mission

Fix the types

~20 min

Quick check

Sorting an amount column gives 100, 1000, 20, 300. What does this tell you?

Go deeper

  1. 1Practice converting a text 'date-looking' column into an actual date type.
  2. 2Learn to spot when a numeric column has been silently imported as text.
  3. 3Check data types immediately after importing any new dataset, as a habit.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge