How to Compare a pandas Date Column with a Python date
A pandas date column holds pandas timestamps, and a Python datetime.date is a different type, so comparing them directly fails with a TypeError. Wrapping the date in pd.Timestamp makes the comparison work. This guide filters rows up to a date with pandas 3.1.2 on Python 3.15.2.
Syntax
<rows> = <table>[<table>["<date column>"] <= pd.Timestamp(<date>)]
| Part | What it means |
|---|---|
<table>["<date column>"] | A column of dates, type datetime64 |
pd.Timestamp(<date>) | Turns a Python date into a pandas timestamp, the same kind of value the column holds |
<= | Any comparison: <, <=, >, >= |
The Timestamp reference describes it as the "Pandas replacement for python datetime.datetime object."
The Example
A table lists three visits on 1 March, 15 March and 2 April 2026. The cutoff, the end of March, is a Python date. The script keeps the visits up to the cutoff.
Prerequisites
- pandas in your project, for example with
uv add pandas. This guide uses pandas 3.1.2 on Python 3.15.2.
Step 1: Write the Script
from datetime import date
import pandas as pd
visits = pd.DataFrame({"day": pd.to_datetime(["2026-03-01", "2026-03-15", "2026-04-02"])})
cutoff = date(2026, 3, 31)
march = visits[visits["day"] <= pd.Timestamp(cutoff)]
print(march)

Step 2: Run It
uv run python compare_dates.py
day
0 2026-03-01
1 2026-03-15

The two March visits stayed; 2 April is after the cutoff. The command works unchanged in PowerShell.
Without pd.Timestamp
The same filter with the plain date, visits["day"] <= cutoff, stops with:
TypeError: Invalid comparison between dtype=datetime64[us] and date
datetime64[us] is the column's type: dates with microsecond resolution. pandas 3 won't compare it with a datetime.date.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Comparing with the date itself | TypeError: Invalid comparison between dtype=datetime64[us] and date | Wrap it: pd.Timestamp(cutoff) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Invalid comparison between dtype=datetime64[us] and date | A Python date on one side of the comparison | Use pd.Timestamp(the_date) |
FAQ
Can I compare with text instead?
Yes. pandas reads date text on the other side of the comparison: visits[visits["day"] <= "2026-03-31"] kept the same two rows. A pd.Timestamp is clearer when the cutoff comes from settings as a date.
What type does the column have?
print(visits["day"].dtype) printed datetime64[us].
Conclusion
visits[visits["day"] <= pd.Timestamp(cutoff)] kept the March visits, while the same comparison with the plain date raised Invalid comparison between dtype=datetime64[us] and date. Turn Python dates into timestamps before comparing them with a column.
Additional Resources
- pandas.Timestamp — creating and using timestamps.
- Time series / date functionality — dates in pandas.
