TutorialsPythonString and Date Operations in pandas

String and Date Operations in pandas

Transform text and date columns at scale — the most-used pandas preprocessing operations

After loading data, the two column types that need the most work are strings (text) and dates. pandas has a .str accessor for string operations and a .dt accessor for datetime operations — letting you apply transformations to an entire column in one line.

Examples

String operations with .str accessor
import pandas as pd

df["City"] = df["City"].str.strip().str.title()
df["Email"] = df["Email"].str.lower()

# Extract parts of strings
df["Domain"] = df["Email"].str.split("@").str[1]
df["FirstName"] = df["Name"].str.split(" ").str[0]

# Check contains / starts with
df[df["City"].str.contains("Delhi", na=False)]
df[df["Code"].str.startswith("NCR")]

# Replace with regex
df["Phone"] = df["Phone"].str.replace(r"D", "", regex=True)  # keep digits only

# Pad strings (useful for IDs)
df["EmpID"] = df["EmpID"].astype(str).str.zfill(5)  # "42" → "00042"

# String length
df["Name_Len"] = df["Name"].str.len()
Date operations with .dt accessor
import pandas as pd

df["OrderDate"] = pd.to_datetime(df["OrderDate"])  # must be datetime first

# Extract date parts
df["Year"]    = df["OrderDate"].dt.year
df["Month"]   = df["OrderDate"].dt.month
df["Day"]     = df["OrderDate"].dt.day
df["Quarter"] = df["OrderDate"].dt.quarter
df["DayName"] = df["OrderDate"].dt.day_name()  # "Monday", "Tuesday"...
df["WeekNum"] = df["OrderDate"].dt.isocalendar().week

# Month name (sorted properly)
df["MonthName"] = df["OrderDate"].dt.strftime("%B")  # "January"
df["YearMonth"] = df["OrderDate"].dt.strftime("%Y-%m")  # "2026-03"

# Date arithmetic
from datetime import date
df["DaysAgo"] = (pd.Timestamp.today() - df["OrderDate"]).dt.days

# Filter by date
df[df["OrderDate"] >= "2026-01-01"]
df[df["OrderDate"].dt.year == 2026]
💡 pd.to_datetime() is always step one for date columns. Without it, .dt accessor raises AttributeError.

Key Points

  • .str accessor enables string methods on an entire column — no loop needed
  • .dt accessor enables datetime methods — requires pd.to_datetime() first
  • str.contains(na=False) avoids errors when NaN values exist in the column
  • strftime format codes: %Y=4-digit year, %m=month number, %B=month name, %d=day
  • Date arithmetic with pd.Timestamp.today() gives age in days for each row

Practice Question

After pd.to_datetime(), how do you extract just the year from a date column "OrderDate"?