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().
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:
| Customer | Age | Salary | JoinDate | Analyst Diagnosis |
|---|---|---|---|---|
| Amit | "24" | "45000" | "2026-01-10" | Text string (object) |
| Priya | "27" | "55000" | "2026-02-15" | Text string (object) |
- Calculations Fail:
"45000" + "5000"produces"450005000"(string concatenation) instead of50000! - Alphabetical Sorting: In text sorting,
"100"comes BEFORE"20"because "1" < "2"! - Filtering Errors: Running
df["Salary"] > 50000raises aTypeError: '>' 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.dtypesdf.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
4. Common Data Types in Pandas
The core data types encountered during enterprise data cleaning
Whole numbers without decimals (e.g. Age, Quantity, OrderID).
Decimal numbers and numeric columns containing NaN values.
Text strings or mixed incompatible types (e.g. Name, City, dirty numbers).
True / False flags (e.g. IsActive, Churned, Eligible).
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():
# 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)
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
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:
# Coerce invalid non-numeric strings into NaN df["Salary"] = pd.to_numeric(df["Salary"], errors="coerce")
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
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
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):
| Employee | Age | Salary | JoinDate | Active |
|---|---|---|---|---|
| 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"→ Usepd.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
Inspect
df.dtypes, df.info()
Identify
Find numeric text
Understand
Scan for dirty strings
Convert
astype / to_numeric
Audit NaNs
df.isna().sum()
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:
16. Common Mistakes to Avoid
Pitfalls made when inspecting and casting data types
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.
If even one record contains "unknown" or "N/A", astype(int) will crash with a ValueError. Use pd.to_numeric(errors="coerce") instead.
Coercing dirty text into NaN prevents crashes, but introduces missing values. Always run df.isna().sum() to see how much data was coerced.
"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
Python Data Types Knowledge Check
Validate your understanding of dtypes, astype(), to_numeric(), errors='coerce', and to_datetime().
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
df.dtypes and df.info().Series.astype().pd.to_numeric(errors="coerce").datetime64[ns] with pd.to_datetime().