Data Cleaning & Type Casting Numerical Integrity Pandas 2.x Verified Live Interactive Lab

Python Data Cleaning: Inspecting & Converting Data Types

Master professional data type inspection, diagnosis, and conversion in Pandas. Learn why numbers stored as text break analytics, how to inspect column types with df.dtypes, convert clean data with astype(), safely coerce dirty values with pd.to_numeric(errors="coerce"), and parse dates with pd.to_datetime().

Level: Beginner to Intermediate
Environment: Python 3 / Pandas 2.x
Est. Time: 50–65 Minutes

1. Why Data Types Matter

When correct-looking values conceal corrupted underlying types

In data analytics, records imported from CSV, JSON, or external APIs frequently look fine on screen, but are internally stored as text strings:

CustomerAgeSalaryJoinDateAnalyst Diagnosis
Amit"24""45000""2026-01-10"Text string (object)
Priya"27""55000""2026-02-15"Text string (object)
⚠️ Why Wrong Types Ruin Analytics:
  • Calculations Fail: "45000" + "5000" produces "450005000" (string concatenation) instead of 50000!
  • Alphabetical Sorting: In text sorting, "100" comes BEFORE "20" because "1" < "2"!
  • Filtering Errors: Running df["Salary"] > 50000 raises a TypeError: '>' not supported between instances of 'str' and 'int'.
  • Aggregations Ignored: Calling df.mean() will silently skip text columns, leaving salary out of your executive summary!

2. Checking Data Types: df.dtypes and df.info()

The first diagnostic step in every data cleaning pipeline

Pandas provides two primary methods to inspect column types:

df.dtypes

Returns a Series displaying the data type of every column at a glance.

df.dtypes

df.info()

Prints an architectural summary including column names, non-null counts, dtypes, and memory usage.

df.info()

3. 🔥 Live Type Inspection

Audit the data types of an uncleaned customer DataFrame

inspect_types.py
Click "Run Python" to test...

4. Common Data Types in Pandas

The core data types encountered during enterprise data cleaning

Integer
int64

Whole numbers without decimals (e.g. Age, Quantity, OrderID).

Float
float64

Decimal numbers and numeric columns containing NaN values.

Object
object

Text strings or mixed incompatible types (e.g. Name, City, dirty numbers).

Boolean
bool

True / False flags (e.g. IsActive, Churned, Eligible).

Datetime
datetime64[ns]

Timestamps and dates supporting calendar filtering and time deltas.

5. Converting Compatible Data Types with astype()

Direct type casting when all column values are clean

When values are already compatible (clean text numbers without symbols or missing words), use astype():

astype() Blueprint
# Convert clean text numbers to integer
df["Age"] = df["Age"].astype(int)

# Convert clean text numbers to decimal float
df["Salary"] = df["Salary"].astype(float)
When is astype() appropriate?
Use astype() ONLY when you are certain every single value in the column is valid. If even one value is "unknown" or "N/A", astype() will immediately crash with a ValueError!

6. 🔥 Live astype() Practice

Convert string columns to int and float and verify dtypes

astype_lab.py
Click "Run Python" to test astype...

7. 🔥 Converting Dirty Numeric Text Safely: pd.to_numeric()

Gracefully handling "unknown", "invalid", and corrupt strings

Real-world columns rarely contain 100% clean numbers. They often have notes like "unknown", "N/A", or "pending". Using pd.to_numeric(..., errors="coerce") solves this:

pd.to_numeric Blueprint
# Coerce invalid non-numeric strings into NaN
df["Salary"] = pd.to_numeric(df["Salary"], errors="coerce")
⚠️ Analyst Invariant: errors="coerce" does NOT magically fix bad data!
It turns unparseable garbage strings into NaN (missing values). After coercing, an analyst must immediately inspectdf.isna().sum() and decide whether to impute (via fillna(median)) or drop!

8. 🔥 Live Numeric Conversion

Convert dirty string columns and observe coerced NaN creation

coerce_numeric_lab.py
Click "Run Python" to test numeric conversion...

9. Converting Dates: pd.to_datetime()

Unlocking true chronological sorting, filtering, and time deltas

Strings like "2026-03-20" are text. When you sort text, alphabetical order prevails, which breaks calendar logic. Converting to datetime64[ns] provides:

Chronological Operations

• Sort orders by true calendar timeline.
• Filter ranges: df[df["Date"] > "2026-02-01"].
• Calculate duration: df["EndDate"] - df["StartDate"].

Date Properties (.dt)

• Extract year: df["Date"].dt.year
• Extract month: df["Date"].dt.month
• Extract day name: df["Date"].dt.day_name()

10. 🔥 Live Date Conversion

Parse date strings and sort orders chronologically

date_conversion_lab.py
Click "Run Python" to test date conversion...

11. astype() vs. to_numeric() vs. to_datetime()

Choosing the right conversion method for each scenario

astype()

df["Col"].astype(int)

Best for: Clean, compatible values. Fast and direct, but crashes if dirty text is present.

pd.to_numeric()

pd.to_numeric(..., errors="coerce")

Best for:Numeric columns with dirty strings (like "unknown"). Safely coerces bad values to NaN.

pd.to_datetime()

pd.to_datetime(df["Date"])

Best for:Date & time strings. Parses dates into datetime64[ns] to unlock time analysis.

12. 🔥 Data Type Cleaning vs. Data Value Cleaning

Never confuse the format with the content

Data Type Problem

• Example: "45000"
• The value is legitimate, but it is stored as text (object).
• Solution: Convert with astype() or pd.to_numeric().

Data Value Problem

• Example: "unknown" or -999 or typo.
• The underlying recorded measurement itself is wrong or unavailable.
• Solution: Coerce to NaN and apply Missing Value imputation.

13. 🔥 Practical Data Analytics Scenario

Diagnosing an enterprise employee onboarding log

You receive this raw HR dataset where every column was parsed as object (string text):

EmployeeAgeSalaryJoinDateActive
Amit"24""45000""2026-01-10""True"
Priya"27""55000""2026-02-15""False"
Rahul"25""unknown""2026-01-25""True"
Neha"29""65000""2026-03-05""True"

Analyst Conversion Strategy:

  • Age: Clean numbers without nulls → Convert using df["Age"].astype(int).
  • Salary: Contains "unknown" → Use pd.to_numeric(df["Salary"], errors="coerce").
  • JoinDate: String dates → Convert using pd.to_datetime(df["JoinDate"]).
  • Active: Boolean strings → Convert using df["Active"].astype(bool).

14. The 6-Step Type-Cleaning Workflow

A reliable framework for data type hygiene

1

Inspect

df.dtypes, df.info()

2

Identify

Find numeric text

3

Understand

Scan for dirty strings

4

Convert

astype / to_numeric

5

Audit NaNs

df.isna().sum()

6

Verify

Check df.dtypes

15. 🔥 Final Live Challenge: Clean Enterprise Types

Execute the complete end-to-end type conversion pipeline

Clean the retail sales dataset below: convert Price safely with errors="coerce", Quantity to numeric, OrderDate to datetime, and verify that df.dtypes reflects the correct types:

final_challenge.py
Click "Run Python" to test your pipeline...

16. Common Mistakes to Avoid

Pitfalls made when inspecting and casting data types

1. Assuming numbers stored as text are automatically numeric

A value like "45000" looks like a number to the human eye, but Pandas treats it as text. It cannot be averaged or sorted numerically without casting.

2. Using astype(int) when dirty strings exist

If even one record contains "unknown" or "N/A", astype(int) will crash with a ValueError. Use pd.to_numeric(errors="coerce") instead.

3. Forgetting to audit new NaNs after errors="coerce"

Coercing dirty text into NaN prevents crashes, but introduces missing values. Always run df.isna().sum() to see how much data was coerced.

4. Confusing type problems with value problems

"45000" is a valid value in the wrong format (type problem). "unknown" is an unrecorded measurement requiring business remediation (value problem).

17. Knowledge Check Quiz

Validate your data type conversion skills

TEST YOUR KNOWLEDGE

Python Data Types Knowledge Check

Validate your understanding of dtypes, astype(), to_numeric(), errors='coerce', and to_datetime().

Question 1 of 4Current Score: 0 / 0
Q1

Why does adding two numeric values stored as text (e.g. "45000" + "5000") produce "450005000" instead of 50000?

18. Key Takeaways Checklist

What you can now do with Python Data Types

Inspect column types with df.dtypes and df.info().
Convert clean numeric strings with Series.astype().
Safely handle invalid strings with pd.to_numeric(errors="coerce").
Parse dates into datetime64[ns] with pd.to_datetime().
Differentiate data-type problems from data-value problems.
Audit new NaNs created by coercion and verify final dtypes.