Skip to content

Raise a TypeError when a Coordinates object is used as a coordinate value - #11523

Open
NoiceHax wants to merge 4 commits into
pydata:mainfrom
NoiceHax:fix/issue-10194
Open

Raise a TypeError when a Coordinates object is used as a coordinate value#11523
NoiceHax wants to merge 4 commits into
pydata:mainfrom
NoiceHax:fix/issue-10194

Conversation

@NoiceHax

Copy link
Copy Markdown

Description

Passing a Coordinates object as a value inside a coords mapping, like ds.assign_coords({"x": coords}), was never caught. It fell through as_variable into as_compatible_data, which treats the mapping as a sequence and builds an array out of its coordinate names. If the number of names happened to match the dimension size you got no error at all, just a coordinate holding the names. Otherwise you got a "conflicting sizes for dimension" error further down that gives no hint about what actually went wrong.

as_variable now checks for a Coordinates object and raises a TypeError telling you to pass it directly with ds.assign_coords(coords). Doing the check there covers assign_coords on both Dataset and DataArray and both constructors in one place.

One existing test, test_drop_attrs, built its multi-index through the broken spelling, so its z coordinate was really a <U1 array of coordinate names rather than a multi-index. It now passes the Coordinates object directly.

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code. I asked it to make xarray raise an error when a Coordinates object is passed as a value in a coords mapping, as described in the issue, and I reviewed and tested the result.

…e value

Passing a Coordinates object as a value in a coords mapping, such as
ds.assign_coords({"x": coords}), fell through to as_compatible_data, which
turned the mapping into an array of its coordinate names. Depending on how
many names it held, that either built a nonsense coordinate or failed later
with a confusing "conflicting sizes" error.

as_variable now detects a Coordinates object and raises a TypeError that
points at ds.assign_coords(coords). Doing the check there covers
assign_coords on Dataset and DataArray as well as both constructors.

test_drop_attrs built its multi-index the broken way, so it is updated to
pass the Coordinates object directly.

Closes pydata#10194

Co-authored-by: Claude <noreply@anthropic.com>
@welcome

welcome Bot commented Aug 15, 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.

NoiceHax and others added 2 commits August 15, 2026 11:55
The typos hook rejects the handle in the attribution line, so add it to
the people's names allowlist alongside the existing entries. Also use the
full dotted path for the Coordinates reference, since the bare name has no
target in api.rst and the docs build runs with fail_on_warning.

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

raise an error for assign_coords({Hashable: xr.Coordinates})

2 participants