TutorialsPythonString and Date Operations in Pandas

String and Date Operations in Pandas

Clean text columns and extract date parts with Pandas str and dt accessors

Two of the most common data cleaning tasks are: fixing messy text in string columns, and extracting useful information from date columns (year, month, day of week). Pandas provides .str accessor for string operations and .dt accessor for datetime operations — both apply to the entire column at once.

Examples

String operations with .str accessor
import pandas as pd

df = pd.DataFrame({
    "City": ["  delhi  ", "NOIDA", "Gurgaon ", "faridabad"],
    "Code": ["PROD-001-DL", "PROD-002-UP", "PROD-003-HR"],
    "Phone": ["9876543210", "9123456789", "8765432109"],
})

# Cleaning
df["City"] = df["City"].str.strip().str.title()
# "  delhi  " → "Delhi"

# Contains / starts / ends
df[df["City"].str.contains("Del", case=False)]  # case-insensitive filter
df[df["Code"].str.startswith("PROD")]

# Extract parts
df["ProductNum"] = df["Code"].str.split("-").str[1]
# "PROD-001-DL" → "001"

df["State"] = df["Code"].str[-2:]
# "PROD-001-DL" → "DL"

df["Phone_Masked"] = df["Phone"].str[:4] + "XXXXXX"
# "9876XXXXXX"

# Replace with regex
df["Code"] = df["Code"].str.replace(r"PROD-0*", "", regex=True)
# "PROD-001-DL" → "1-DL"
Date operations with .dt accessor
import pandas as pd

df = pd.DataFrame({
    "OrderDate": pd.to_datetime(["2026-01-15", "2026-03-22", "2026-08-05"]),
    "Amount": [45000, 18000, 62000],
})

# Extract date parts
df["Year"]    = df["OrderDate"].dt.year     # 2026
df["Month"]   = df["OrderDate"].dt.month    # 1, 3, 8
df["Month_Name"] = df["OrderDate"].dt.month_name()  # January, March, August
df["Quarter"] = df["OrderDate"].dt.quarter  # 1, 1, 3
df["DayOfWeek"] = df["OrderDate"].dt.day_name()  # Thursday, Sunday, Wednesday
df["Week"]    = df["OrderDate"].dt.isocalendar().week

# Date arithmetic
df["Days_Since"] = (pd.Timestamp.today() - df["OrderDate"]).dt.days

# Filter by date
df[df["OrderDate"] >= "2026-03-01"]
df[df["OrderDate"].dt.month == 8]  # August only
df[df["OrderDate"].dt.dayofweek < 5]  # weekdays only (0=Mon, 6=Sun)
💡 Always convert date columns with pd.to_datetime() before using .dt — string columns do not have the .dt accessor.

Key Points

  • .str accessor works on any object (text) column — mirrors Python string methods
  • .str.split("-").str[1] splits and takes the second part — very useful for parsing codes
  • .dt accessor works on datetime columns — gives year, month, quarter, day_name, etc.
  • pd.to_datetime() converts text to datetime — errors="coerce" handles bad values as NaT
  • Date filtering: df[df["Date"] >= "2026-01-01"] — Pandas accepts string dates in comparisons

Practice Question

A "City" column has values like " DELHI ". Which Pandas operation makes it "Delhi"?

Related Topics

String Operations in PythonSlice, split, replace, strip and format text data — essential for cleaning messy datasetsData Cleaning with PandasRename columns, fix data types, remove duplicates, and standardise messy real-world dataFeature Engineering for AnalystsCreate new meaningful columns from existing data to improve analysis and modelling