Skip to content

Ignore datetime/timedelta unit differences in index equality - #11952

Open
ved1j wants to merge 1 commit into
pydata:mainfrom
ved1j:fix-datetime-index-equals-units
Open

ved1j wants to merge 1 commit into
pydata:mainfrom
ved1j:fix-datetime-index-equals-units

Conversation

@ved1j

@ved1j ved1j commented Nov 29, 2026

Copy link
Copy Markdown

Description

With pandas < 3, DatetimeIndex.equals / TimedeltaIndex.equals return False when two indexes hold identical values with different units (e.g. datetime64[s] vs datetime64[ns]). Because PandasIndex.equals and IndexVariable._data_equals delegate to pandas, DataArray.equals on coordinates disagreed with broadcast_equals and with how data variables are compared (#10045). Still reproduces on main with pandas 2.3.3, which is the minimum supported version; pandas 3 already compares these as equal.

This adds a small pandas_index_equals helper in xarray/core/indexes.py that casts both datetime (or both timedelta) indexes to the finer unit before comparing, and uses it in PandasIndex.equals and IndexVariable._data_equals. If a value in the coarser index is out of bounds for the finer unit, it cannot match any value there, so the comparison returns False.

Tested locally with the full test suite on pandas 2.3.3 and 3.1.1 (numpy 2.4); the new tests fail on main with pandas 2.3.

Checklist

AI Disclosure

[This is Claude Code on behalf of ved1j]

🤖 Generated with Claude Code

With pandas < 3, DatetimeIndex.equals and TimedeltaIndex.equals return
False for indexes with identical values but different units (e.g. "s"
vs "ns"). This made DataArray.equals / Dataset.equals disagree with
broadcast_equals and with the behavior for data variables. Cast both
indexes to the finer unit before comparing.

Closes pydata#10045

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@welcome

welcome Bot commented Nov 29, 2026

Copy link
Copy Markdown

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Equality between datetime64[s] and datetime64[ns] is not consistent for coordinates vs data variables

1 participant