Call the .date() method on a datetime.datetime instance (dt.date()) to extract a datetime.date object (containing only year, month, and day with time and timezone components stripped), or use pd.to_datetime(s).dt.date / .dt.floor("D") when extracting dates across a Pandas Series.
Basic Example
from datetime import datetime, timezone
# 1. Recommended: Call .date() on any datetime object
dt = datetime(2026, 11, 29, 14, 35, 10)
d = dt.date()
print(d) # Output: 2026-11-29
print(type(d)) # Output: <class 'datetime.date'>
# 2. Timezone-aware datetime (convert to target timezone BEFORE calling .date()!):
utc_dt = datetime(2026, 11, 29, 22, 30, tzinfo=timezone.utc)
print(utc_dt.date()) # Output: 2026-11-29
Key Details & Edge Cases
datetimeis a subclass ofdate(isinstance(dt, date)isTrue!), yetdt == disFalse:- Because
datetime.datetimeinherits fromdatetime.date,isinstance(datetime.now(), date)evaluates toTrue, which can hide bugs if you pass adatetimeinto a function expecting adate. - Crucially, comparing a
datetimedirectly to adate(datetime(2026, 11, 29, 0, 0) == date(2026, 11, 29)) always returnsFalse, anddt < draisesTypeError: can't compare datetime.datetime to datetime.date. Always call.date()on both sides before comparing.
- Because
- Timezone caveat:
.date()dropstzinfowithout converting:dt.date()extracts the calendar date indt’s current timezone (2026-11-29 22:30 UTC$\rightarrow$2026-11-29, even though at the same instant in PKT+05:00it is already2026-11-30 03:30). Convert to the user’s local timezone first (dt.astimezone(ZoneInfo("Asia/Karachi")).date()) before calling.date().
- Calling
.date()on an object that is already adatetime.dateraisesAttributeError:date(2026, 11, 29).date()fails withAttributeError: 'datetime.date' object has no attribute 'date'. If an input might be eitherdateordatetime, normalize safely withval.date() if isinstance(val, datetime) else val.
- Pandas Series / DataFrame column (
s.dt.datevs.s.dt.normalize()/s.dt.floor("D")):df["ts"].dt.dateproduces anobject-dtype Series of Pythondatetime.dateinstances (slower for downstream Pandas date arithmetic and grouping). When staying inside Pandas, preferdf["ts"].dt.floor("D")(or.dt.normalize()), which sets the time to00:00:00while preserving fast nativedatetime64[ns]vectorization.
Comparison of Date-Extraction Methods in Python & Pandas
| Method | Input Type | Return Type | Preserves Vectorized datetime64? |
Best Used When |
|---|---|---|---|---|
dt.date() (Recommended) |
datetime.datetime |
datetime.date |
N/A (scalar Python object) | Extracting a standard date object for dictionary keys, SQL parameters, or date comparisons. |
dt.astimezone(tz).date() |
Aware datetime.datetime |
datetime.date |
N/A (scalar Python object) | Getting the local calendar date in a specific timezone from a UTC timestamp. |
s.dt.date |
Pandas datetime64 Series |
pd.Series (object dtype) |
No (stores Python date objects) |
Exporting pure datetime.date objects to CSV/JSON or passing to external Python APIs. |
s.dt.floor("D") / s.dt.normalize() |
Pandas datetime64 Series |
pd.Series (datetime64[ns]) |
Yes (midnight 00:00:00 timestamps) |
Grouping, filtering, or joining by calendar day inside Pandas at full C/Arrow speed. |
You must log in or register to comment.
