Menu

Correcting Data Types and Invalid Values

Correcting Data Types and Invalid Values

Data imported from external sources may contain numbers stored as text, dates represented as strings, or categorical values encoded inconsistently. Correcting these issues ensures that operations behave as expected.

Converting Data Types

import pandas as pd
df = pd.DataFrame({
"Product": ["Laptop", "Phone", "Tablet"],
"Price": ["55000", "30000", "25000"],
"Quantity": ["10", "15", "12"]
})
df["Price"] = pd.to_numeric(df["Price"], errors="coerce")
df["Quantity"] = pd.to_numeric(df["Quantity"], errors="coerce")
print(df.dtypes)

The pd.to_numeric() function converts valid numerical strings into numeric values. Using errors="coerce" converts invalid entries to missing values, which should then be inspected and handled appropriately.

Converting Date Values

Dates are often imported as strings. Converting them to a datetime type allows you to extract years, months, and time intervals.

df = pd.DataFrame({
"Order_Date": ["2026-01-10", "2026-02-15", "invalid"]
})
df["Order_Date"] = pd.to_datetime(
df["Order_Date"],
errors="coerce"
)
print(df)
print(df["Order_Date"].dtype)

The invalid date becomes NaT, which represents a missing datetime value in Pandas.

You can extract date components after conversion:

df["Order_Year"] = df["Order_Date"].dt.year

df["Order_Month"] = df["Order_Date"].dt.month

print(df)

Validating Numerical Ranges

Validation rules help identify values that fall outside an expected range.

df = pd.DataFrame({
"Age": [25, 30, 150, -5, 42]
})
invalid_ages = df[
(df["Age"] < 0) | (df["Age"] > 120)
]
print(invalid_ages)

This example flags ages below zero or above 120. The range is illustrative; actual validation rules should reflect the requirements of the dataset.

Flag suspicious records before deciding whether to correct, remove, or retain them. When possible, compare them with the original source.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details