Back to Python Notes
Topic #263

Wrong Format and Wrong Data

Learn how to identify and correct data type mismatches and outliers in Pandas DataFrames to ensure accurate analysis.

What it is

Data cleaning involves transforming raw data into a structured format suitable for analysis. Two common issues are wrong formats (e.g., dates stored as strings) and wrong data (e.g., negative ages or extreme outliers). Correcting types ensures operations like sorting or arithmetic work correctly, while handling outliers prevents skewed statistical results. Related terms include type casting, data validation, and outlier detection.

Why it matters

  • Accurate Calculations: You cannot perform date arithmetic on string objects.
  • Memory Efficiency: Proper numeric types use less memory than object types.
  • Reliable Visualizations: Outliers can distort chart scales, hiding trends.
  • Model Performance: Machine learning models require consistent, clean input features.

Syntax or steps

  1. Inspect Types: Use df.dtypes to check current column types.
  2. Convert Formats: Use specific converters like pd.to_datetime() or .astype().
  3. Detect Outliers: Calculate statistics (mean, std dev) or use IQR (Interquartile Range).
  4. Handle Outliers: Remove rows, cap values, or replace with median/NaN.

Example

import pandas as pd
import numpy as np

# Sample data with wrong format (date as string) and outlier (age 150)
data = {
    "name": ["Alice", "Bob", "Charlie"],
    "date_str": ["2023-01-01", "2023-02-15", "2023-03-20"],
    "age": [25, 150, 30]
}
df = pd.DataFrame(data)

# 1. Fix Wrong Format: Convert string to datetime
df["date"] = pd.to_datetime(df["date_str"])

# 2. Identify Outliers using IQR method for 'age'
Q1 = df["age"].quantile(0.25)
Q3 = df["age"].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR

# 3. Handle Wrong Data: Replace outlier age with NaN
df.loc[(df["age"] < lower_bound) | (df["age"] > upper_bound), "age"] = np.nan

print(df)

This code first converts the date_str column into a proper datetime object. Then, it calculates the Interquartile Range (IQR) for ages. Any age outside the range defined by $1.5 \times IQR$ from the quartiles is considered an outlier and replaced with NaN.

Common mistakes

  • Ignoring Errors: Using errors='coerce' in to_datetime silently turns bad dates into NaT; always check how many were converted.
  • Hardcoding Thresholds: Assuming all datasets have the same valid ranges without checking distribution.
  • Deleting Too Much: Removing entire rows for single-column outliers may lose valuable data; consider imputation instead.
  • Forgetting Indexes: After filtering or replacing, indexes may become non-contiguous; use reset_index(drop=True) if needed.

When to use it

ScenarioActionReason
Date/Time Analysispd.to_datetime()Enables time-based sorting and grouping.
Numeric Calculation.astype(float)Strings cannot be summed or averaged.
Extreme ValuesIQR or Z-ScoreStatistically identifies anomalies vs. normal variation.
Missing DataImputation/DropPrevents errors in algorithms that don't handle NaN.

Practice

Guided Exercise: Create a DataFrame with a column "price" containing strings like "$100". Convert them to floats by removing the dollar sign.

Challenge: Given a list of temperatures [20, 22, 21, 100, 23], use the mean and standard deviation to identify the outlier. Hint: Calculate $z = \frac{x - \mu}{\sigma}$.

Quick check

Question: Why might you choose to replace an outlier with the median rather than deleting the row?

Answer: Deleting the row removes all other valid data in that record. Replacing with the median preserves the rest of the information while mitigating the skew caused by the extreme value.

Summary

Correcting data types and managing outliers are foundational steps in data preprocessing. Always validate your transformations to ensure they align with domain knowledge and statistical expectations.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Wrong Format and Wrong Data – FAQs

Quick answers about learning Wrong Format and Wrong Data in Python.

This free note from Coding Now Tech Institute explains Wrong Format and Wrong Data in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Wrong Format and Wrong Data, is 100% free with no signup required.
With focused practice, most students grasp Wrong Format and Wrong Data in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now