Dates in Python – Complete Guide for Data Science 2026
The date class from Python’s datetime module is the foundation for working with calendar dates in data science. Whether you need to extract the current date, create specific dates for feature engineering, compare dates, or perform date arithmetic, mastering date objects is essential for clean, reliable, and timezone-aware time-based analysis.
TL;DR — Core Date Operations
date.today()→ current datedate(year, month, day)→ create specific date.year,.month,.day,.weekday()→ extract componentstimedeltafor date arithmetic
1. Creating and Getting Dates
from datetime import date
# Current date
today = date.today()
print(today)
# Specific date
birthday = date(2026, 3, 19)
print(birthday)
# From string (common in data files)
from datetime import datetime
dt = datetime.strptime("2026-03-19", "%Y-%m-%d").date()
print(dt)
2. Extracting Date Components
d = date(2026, 3, 19)
print(f"Year: {d.year}")
print(f"Month: {d.month}")
print(f"Day: {d.day}")
print(f"Weekday number: {d.weekday()} (0 = Monday)")
print(f"Day name: {d.strftime('%A')}")
print(f"ISO format: {d.isoformat()}")
3. Real-World Data Science Examples
import pandas as pd
df = pd.read_csv("sales_data.csv", parse_dates=["order_date"])
# Convert to pure date (drop time component)
df["order_date_only"] = df["order_date"].dt.date
# Extract useful date features
df["order_year"] = df["order_date_only"].apply(lambda x: x.year)
df["order_month"] = df["order_date_only"].apply(lambda x: x.month)
df["order_day"] = df["order_date_only"].apply(lambda x: x.day)
df["is_weekend"] = df["order_date_only"].apply(lambda x: x.weekday() in (5, 6))
4. Date Arithmetic
from datetime import timedelta
today = date.today()
# Add/subtract days
tomorrow = today + timedelta(days=1)
last_week = today - timedelta(weeks=1)
# Days between two dates
delta = birthday - today
print(f"Days until birthday: {delta.days}")
5. Best Practices in 2026
- Use
datewhen you only need the date part (no time) - Always combine with
datetimeandzoneinfofor full timestamp needs - Use pandas
.dt.datefor vectorized conversion on DataFrames - Store dates in UTC where possible and convert for display
- Use
dateobjects in sets or as dict keys (they are hashable)
Conclusion
The date class in Python is simple yet powerful for all calendar-date operations in data science. In 2026, use it for clean feature engineering, date comparisons, and any situation where you only need the date part without time. Combine it with timedelta for easy date arithmetic and pandas for large-scale processing.
Next steps:
- Explore one of your datasets containing datetime columns and extract pure date components for new features