| name | python-datetime |
| description | Work with dates, times, and timezones to this project's standards. Use when creating, parsing, serialising, comparing, or storing datetimes, or handling timezones. |
Python Datetime
Standards for dates, times, and timezones, using the standard library datetime and
zoneinfo.
Timezones
- Always use timezone-aware datetimes; never naive ones. Attach a zone at the boundary
where a value enters the system.
- Use the standard library
zoneinfo for timezones, not pytz (whose legacy
localize() dance is a frequent source of bugs).
Serialising and parsing
- Serialise with ISO 8601 via
isoformat() and parse with fromisoformat() — not
strftime/strptime with a hand-written format, and not all-digit numeric formats.
- When parsing datetimes in pandas, pass an explicit
format= to pd.to_datetime rather
than relying on inference.
Pitfalls
- Be careful using datetimes as dict keys or set members: two values for the same instant
can carry different
tzinfo objects and hash differently, so a lookup silently misses.
Normalise to one zone (e.g. UTC) first.
See python-pandas for typing DataFrame timestamp columns as pd.Timestamp.