SyntaxStudy
Sign Up
Python Datetime Performance Tips
Python Advanced 4 min read

Datetime Performance Tips

Performance

datetime operations can be slow at scale. Cache timezone objects, avoid parsing in loops, and use pandas for bulk operations.

Example
from zoneinfo import ZoneInfo
from datetime import datetime

# Cache timezone object (reuse, not re-instantiate)
UTC  = ZoneInfo("UTC")
NY   = ZoneInfo("America/New_York")

# Bulk conversion with pandas (much faster than loop)
import pandas as pd
ts_series = pd.Series([1718456400, 1718542800, 1718629200])
datetimes = pd.to_datetime(ts_series, unit="s", utc=True)
# Avoid strptime in tight loops — parse once, add timedelta
base = datetime(2024, 1, 1, tzinfo=UTC)
dates = [base + timedelta(i) for i in range(365)]
Pro Tip

pd.to_datetime() on a Series is ~100x faster than a list comprehension of datetime.fromtimestamp().