Raise a TypeError when a Coordinates object is used as a coordinate value - #11523
Open
NoiceHax wants to merge 4 commits into
Open
Raise a TypeError when a Coordinates object is used as a coordinate value#11523NoiceHax wants to merge 4 commits into
NoiceHax wants to merge 4 commits into
Conversation
…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>
|
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
for more information, see https://pre-commit.ci
dcherian
approved these changes
Aug 15, 2026
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>
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Passing a
Coordinatesobject as a value inside a coords mapping, likeds.assign_coords({"x": coords}), was never caught. It fell throughas_variableintoas_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_variablenow checks for aCoordinatesobject and raises aTypeErrortelling you to pass it directly withds.assign_coords(coords). Doing the check there coversassign_coordson bothDatasetandDataArrayand both constructors in one place.One existing test,
test_drop_attrs, built its multi-index through the broken spelling, so itszcoordinate was really a<U1array of coordinate names rather than a multi-index. It now passes theCoordinatesobject directly.Checklist
assign_coords({Hashable: xr.Coordinates})#10194whats-new.rstAI Disclosure
Tools: Claude Code. I asked it to make xarray raise an error when a
Coordinatesobject is passed as a value in a coords mapping, as described in the issue, and I reviewed and tested the result.