Skip to content

Conversation

@sachinn854
Copy link
Contributor

@sachinn854 sachinn854 commented Jan 1, 2026

When comparing temporal dtypes (Date / Datetime / Duration / Time)
against non-temporal values, Polars previously raised:

"Series of type <dtype> does not have eq operator"

This error was misleading, because the operator does exist — the
comparison is simply invalid due to incompatible types.

This change improves the error message to explicitly state that a
temporal value is being compared against a non-temporal dtype, e.g.:

TypeError:
Invalid comparison between temporal dtype 'Date'
and non-temporal value of dtype <class 'int'>.
Temporal values may only be compared against
compatible temporal dtypes.

This makes the behavior clearer and more consistent with user
expectations when performing mixed-dtype comparisons.

Closes #25729

Copilot AI review requested due to automatic review settings January 1, 2026 15:21
@sachinn854 sachinn854 changed the title python: improve error message when comparing temporal and non-temporal dtypes (#25729) python: clearer error when comparing temporal and non-temporal dtypes Jan 1, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the error message when comparing temporal dtypes (Date, Datetime, Duration, Time) with non-temporal values. Previously, the error was generic: "Series of type Date does not have eq operator". The new error is more descriptive: "Invalid comparison between temporal dtype Date and non-temporal value of dtype <class 'int'>. Temporal values may only be compared against compatible temporal dtypes."

Key changes:

  • Added a specific error check for temporal vs non-temporal comparisons before falling back to the generic "does not have operator" error
  • The check uses is_temporal() method to identify temporal dtypes and distinguish them from other types

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if f is None:
other_dtype = getattr(other, "dtype", type(other))
if (
self.dtype.is_temporal()
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The temporal dtype check should exclude None values. None/null values should be comparable with any dtype, including temporal dtypes. If the FFI function lookup fails for a None comparison, this check would incorrectly raise a TypeError categorizing None as a "non-temporal value". Add and other is not None to the condition on line 834 before checking if the other value is non-temporal.

Suggested change
self.dtype.is_temporal()
self.dtype.is_temporal()
and other is not None

Copilot uses AI. Check for mistakes.
Comment on lines +833 to +843
other_dtype = getattr(other, "dtype", type(other))
if (
self.dtype.is_temporal()
and not getattr(other_dtype, "is_temporal", lambda: False)()
):
msg = (
"Invalid comparison between temporal dtype "
f"{self.dtype!r} and non-temporal value of dtype {other_dtype!r}. "
"Temporal values may only be compared against compatible temporal dtypes."
)
raise TypeError(msg)
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new error message should have test coverage to ensure it's raised correctly when comparing temporal dtypes (Date, Datetime, Duration, Time) with non-temporal scalar values (int, str, float, etc.). Consider adding tests that verify the error message is shown for each temporal dtype when compared with various non-temporal types.

Copilot uses AI. Check for mistakes.
@sachinn854 sachinn854 changed the title python: clearer error when comparing temporal and non-temporal dtypes python: improve error message for temporal vs non-temporal comparison Jan 1, 2026
@sachinn854 sachinn854 changed the title python: improve error message for temporal vs non-temporal comparison python: Improve error message for temporal vs non-temporal comparison Jan 1, 2026
@sachinn854 sachinn854 changed the title python: Improve error message for temporal vs non-temporal comparison fix: Improve error message for temporal vs non-temporal comparison Jan 1, 2026
@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars and removed title needs formatting labels Jan 1, 2026
@sachinn854
Copy link
Contributor Author

The CI failures are coming from the delta-lake IO tests:

  • tests/unit/io/test_delta.py::test_write_delta
  • tests/unit/io/test_delta.py::test_scan_delta_loads_aws_profile_endpoint_url

They are related to file: vs file:// uri handling / S3 endpoint checks
and seem unrelated to this PR change (which only updates the temporal vs
non-temporal comparison error message).

Keeping this here for visibility — maintainer guidance appreciated if any
action is needed on my side.

@yonikremer
Copy link
Contributor

I fixed the issue that causes the tests to fail, update your branch an rerun the github actions and the tests should pass

@sachinn854 sachinn854 force-pushed the improve-temporal-compare-error-v2 branch from 658a9d3 to bdb544a Compare January 2, 2026 14:17
@codecov
Copy link

codecov bot commented Jan 2, 2026

Codecov Report

❌ Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.51%. Comparing base (355ffac) to head (bdb544a).
⚠️ Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
py-polars/src/polars/series/series.py 25.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25927      +/-   ##
==========================================
- Coverage   80.52%   80.51%   -0.02%     
==========================================
  Files        1764     1765       +1     
  Lines      243008   242842     -166     
  Branches     3045     3046       +1     
==========================================
- Hits       195687   195514     -173     
- Misses      46537    46543       +6     
- Partials      784      785       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sachinn854
Copy link
Contributor Author

All CI tests are passing now.

The Codecov warning is only from the new temporal vs non-temporal
error-message branch not being hit in tests — behavior change is minimal.

Please let me know if you'd like me to add a small unit test for coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fix python Related to Python Polars rust Related to Rust Polars

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad error message when comparing temporal to non-temporal dtype

2 participants