Skip to content

Conversation

RobbieKiwi
Copy link
Contributor

@RobbieKiwi RobbieKiwi commented Sep 9, 2025

Closes #450

Previous logic

For as_data_array when coords is provided:

  • If array is constant, broadcast to coords
  • If array is pandas or xarray, ignore coords and use coords of array
  • If array matches coords, then keep coords

For a more concrete example, if you provide 2 dimensional coords, this is the result of different input data:

  • 0 dimensions -> 2 dimensions
  • 1 dimension -> 1 dimension
  • 2 dimensions -> 2 dimensions

The behavior of 1 dimension -> 1 dimension is clearly the odd one out and is not very intuitive.

This behavior was noticed by people using m.create_variable which calls as_data_array under the hood

Changes proposed in this Pull Request

Add the force_broadcast option to as_data_array . When true, it will always try to broadcast to the dimensions implied by coords.
For the example above this means

  • 0 dimensions -> 2 dimensions
  • 1 dimension -> 2 dimensions
  • 2 dimensions -> 2 dimensions

Use this new option inside m.add_variable so that variable creation is more intuitive.

Note that this is a breaking change for anyone who was relying on the previous behavior when creating variables (I.e they were depending on the coords argument to be ignored). This seems like quite an edge case so perhaps it's OK to change directly.

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

@FabianHofmann FabianHofmann self-requested a review September 21, 2025 20:09
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.

Inconsistent creation of dimensions in add_variables
1 participant