All datetime operations in Pandas – Complete Guide for Data Science 2026
Pandas provides one of the most powerful and complete toolkits for working with datetime data. From parsing and loading to component extraction, timezone conversion, arithmetic, rounding, resampling, and rolling windows, the .dt accessor combined with pd.to_datetime, Timedelta, and resample() covers virtually every time-based operation you will encounter in real-world data science.
TL;DR — Core Pandas Datetime Operations
- Parsing:
parse_dates,pd.to_datetime(),date_format - Components:
.dt.year,.dt.month,.dt.day_name(), etc. - Timezone:
.dt.tz_localize(),.dt.tz_convert() - Arithmetic:
+/-withTimedelta - Rounding:
.dt.floor(),.dt.ceil(),.dt.round() - Resampling & Rolling:
.resample(),.rolling()
1. Parsing and Loading Datetimes
import pandas as pd
df = pd.read_csv("sales_data.csv",
parse_dates=["order_date", "delivery_date"],
date_format="%Y-%m-%d %H:%M:%S",
utc=True)
2. Component Extraction with .dt Accessor
df["year"] = df["order_date"].dt.year
df["month"] = df["order_date"].dt.month
df["day_name"] = df["order_date"].dt.day_name()
df["hour"] = df["order_date"].dt.hour
df["is_weekend"] = df["order_date"].dt.weekday.isin([5, 6])
3. Timezone Operations
df["order_date_utc"] = df["order_date"].dt.tz_localize("UTC")
df["order_date_ny"] = df["order_date_utc"].dt.tz_convert("America/New_York")
4. Arithmetic and Timedelta
now = pd.Timestamp.now(tz="UTC")
df["hours_since_order"] = (now - df["order_date_utc"]).dt.total_seconds() / 3600
df["projected_delivery"] = df["order_date_utc"] + pd.Timedelta(days=14)
5. Rounding, Flooring & Normalization
df["hour_start"] = df["order_date"].dt.floor("H")
df["day_start"] = df["order_date"].dt.normalize()
df["month_end"] = df["order_date"].dt.ceil("M")
6. Resampling and Rolling Windows
df = df.set_index("order_date_utc")
daily_sales = df.resample("D")["amount"].sum()
rolling_7d = df["amount"].rolling("7D").mean()
7. Best Practices in 2026
- Parse datetimes as early as possible with
parse_datesanddate_format - Store all internal timestamps in UTC
- Use the
.dtaccessor for all component extraction and transformations - Perform arithmetic with
Timedeltaon timezone-aware data - Use
resample()androlling()for time-series aggregation and windows
Conclusion
Pandas offers a complete and highly performant set of datetime operations that cover every stage of the data science workflow. From loading and parsing to component extraction, timezone handling, arithmetic, rounding, resampling, and rolling windows — the combination of .dt, Timedelta, resample(), and rolling() gives you everything you need to turn raw timestamps into powerful, actionable insights.
Next steps:
- Review your current datasets and apply the full range of Pandas datetime operations to extract maximum value from your time-based data