Skip to content

Conversation

@max-sixty
Copy link
Collaborator

Fixes #10835

Summary

After PR #10602, Dataset.map started failing when functions returned non-DataArray values (e.g., scalars), raising AttributeError: 'int' object has no attribute 'coords' when trying to access .coords on the returned values.

This PR restores backward compatibility by converting non-DataArray outputs to DataArrays before accessing their .coords attribute.

Changes

  • Modified Dataset.map to check if returned values are DataArrays and convert them if not
  • Added comprehensive test coverage for non-DataArray outputs (scalars, numpy arrays, mixed cases)

Test plan

  • Added test_map_non_dataarray_outputs covering scalar outputs, numpy arrays, and mixed cases
  • All existing Dataset.map tests pass
  • All tests in test_dataset.py pass (495 passed)
  • Pre-commit lints pass

🤖 Generated with Claude Code

After PR pydata#10602, Dataset.map started failing when functions returned
non-DataArray values (e.g., scalars), raising AttributeError when
trying to access .coords on the returned values.

This restores backward compatibility by converting non-DataArray
outputs to DataArrays, which was the behavior before the regression.

Fixes pydata#10835

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Comment on lines +6934 to +6938
# Convert non-DataArray values to DataArrays
variables = {
k: v if isinstance(v, DataArray) else DataArray(v)
for k, v in variables.items()
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

would it make sense to wrap the maybe_wrap_array call above with a ensure_dataarray call (that would be a new function)? That way we don't have to iterate over the variables again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

v reasonable; I'll merge this but agree that would be a smidge faster...

@max-sixty max-sixty merged commit 3572f4e into pydata:main Oct 10, 2025
37 checks passed
@max-sixty max-sixty deleted the map-non-array branch October 10, 2025 22:13
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.

Dataset.map no longer handles non-DataArray outputs

3 participants