Skip to main content

How to Compare a pandas Date Column with a Python date

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<rows> = <table>[<table>["<date column>"] <= pd.Timestamp(<date>)]
PartWhat 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​

compare_dates.py
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)

The compare_dates.py script in a macOS-style code window: a visits DataFrame with three dates, cutoff = date(2026, 3, 31), and the rows where day is at most pd.Timestamp(cutoff).

Step 2: Run It​

Terminal
uv run python compare_dates.py
Output
day
0 2026-03-01
1 2026-03-15

The terminal after uv run python compare_dates.py: the two March visits, 2026-03-01 and 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:

Output (last line)
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​

MistakeWhat happensDo this instead
Comparing with the date itselfTypeError: Invalid comparison between dtype=datetime64[us] and dateWrap it: pd.Timestamp(cutoff)

Troubleshooting​

SymptomLikely causeFix
Invalid comparison between dtype=datetime64[us] and dateA Python date on one side of the comparisonUse 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​