Skip to content

Better ordering of coords in repr#11091

Merged
jsignell merged 6 commits intopydata:mainfrom
jsignell:ordered-coords
Jan 20, 2026
Merged

Better ordering of coords in repr#11091
jsignell merged 6 commits intopydata:mainfrom
jsignell:ordered-coords

Conversation

@jsignell
Copy link
Member

@jsignell jsignell commented Jan 16, 2026

Update coords order in repr based off @ianhi's suggestions in #11039. This undoes a bunch of the docstring changes that came in on #10778 but the order feels more intuitive to me with this PR. All of the original issues are still improved:

Examples

From #712:

In [1]: from collections import *
   ...: import numpy as np
   ...: import xarray as xr
   ...: 
   ...: d1 = xr.DataArray(np.empty((2, 2)), coords=OrderedDict([("foo", [0, 1]), ("bar", [0, 1])]))
   ...: d2 = xr.DataArray(np.empty((2, 2)), coords=OrderedDict([("bar", [0, 1]), ("foo", [0, 1])]))
   ...: 
   ...: ds = xr.Dataset({"d1": d1, "d2": d2})
   ...: 
   ...: print(ds.d1)
<xarray.DataArray 'd1' (foo: 2, bar: 2)> Size: 32B
array([[8.66714752e-277, 9.89251937e-046],
       [4.12632174e+106, 5.37987067e-002]])
Coordinates:
  * foo      (foo) int64 16B 0 1
  * bar      (bar) int64 16B 0 1

In [2]: print(ds.d2)
<xarray.DataArray 'd2' (bar: 2, foo: 2)> Size: 32B
array([[ 8.66714752e-277, -9.89251937e-046],
       [-4.12632174e+106, -5.37987067e-002]])
Coordinates:
  * bar      (bar) int64 16B 0 1
  * foo      (foo) int64 16B 0 1

From #4515

In [1]: import xarray as xr
   ...: import numpy as np
   ...: 
   ...: ds = xr.Dataset()
   ...: ds.coords["as"] = 10
   ...: ds["var"] = xr.DataArray(np.ones((10,)), dims="x", coords={"x": np.arange(10)})
   ...: ds
Out[1]: 
<xarray.Dataset> Size: 168B
Dimensions:  (x: 10)
Coordinates:
  * x        (x) int64 80B 0 1 2 3 4 5 6 7 8 9
    as       int64 8B 10
Data variables:
    var      (x) float64 80B 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0

From #11039

In [1]: import numpy as np
   ...: import xarray as xr
   ...: 
   ...: ds = xr.Dataset(
   ...:     {"data": (("a", "b", "c"), np.random.rand(3, 3, 3))},
   ...:     coords={"a": [0, 1, 2], "b": [0, 1, 2], "c": [3, 2, 1]},
   ...: )
   ...: 
   ...: ds = ds.assign_coords({"a_1":('a', [5,6,7])})
   ...: ds = ds.assign_coords({"b_1":('b', [5,6,7])})
   ...: ds = ds.assign_coords({"c_1":('c', [5,6,7])})
   ...: print(ds)
<xarray.Dataset> Size: 360B
Dimensions:  (a: 3, b: 3, c: 3)
Coordinates:
  * a        (a) int64 24B 0 1 2
    a_1      (a) int64 24B 5 6 7
  * b        (b) int64 24B 0 1 2
    b_1      (b) int64 24B 5 6 7
  * c        (c) int64 24B 3 2 1
    c_1      (c) int64 24B 5 6 7
Data variables:
    data     (a, b, c) float64 216B 0.9063 0.6277 0.8161 ... 0.7899 0.1621 0.247
In [1]: import pandas as pd
   ...: import xarray as xr
   ...: 
   ...: midx = pd.MultiIndex.from_product([["a", "b"], [1, 2]], names=("foo", "bar"))
   ...: ds = xr.Dataset(coords=xr.Coordinates.from_pandas_multiindex(midx, "x"))
   ...: ds.assign_coords(y=[1, 2])
Out[1]: 
<xarray.Dataset> Size: 112B
Dimensions:  (x: 4, y: 2)
Coordinates:
  * x        (x) object 32B MultiIndex
  * foo      (x) object 32B 'a' 'a' 'b' 'b'
  * bar      (x) int64 32B 1 2 1 2
  * y        (y) int64 16B 1 2
Data variables:
    *empty*

return (dims.index(name), 0)

# Non-dimension coordinates sorted by their last dim come second (1)
for d in var.dims[::-1]:
Copy link
Member Author

@jsignell jsignell Jan 16, 2026

Choose a reason for hiding this comment

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

I switched this to look at dims in reverse order because this feels like the right order:

        >>> import numpy as np
        >>> import xarray as xr
        >>> temperature = np.arange(25).reshape(5, 5)
        >>> pressure = np.arange(50, 75).reshape(5, 5)
        >>> da = xr.DataArray(
        ...     data=temperature,
        ...     dims=["x", "y"],
        ...     coords=dict(
        ...         lon=("x", np.arange(10, 15)),
        ...         lat=("y", np.arange(20, 25)),
        ...         Pressure=(["x", "y"], pressure),
        ...     ),
        ...     name="Temperature",
        ... )
        >>> da
        <xarray.DataArray 'Temperature' (x: 5, y: 5)> Size: 200B
        array([[ 0,  1,  2,  3,  4],
               [ 5,  6,  7,  8,  9],
               [10, 11, 12, 13, 14],
               [15, 16, 17, 18, 19],
               [20, 21, 22, 23, 24]])
        Coordinates:
            lon       (x) int64 40B 10 11 12 13 14
            lat       (y) int64 40B 20 21 22 23 24
            Pressure  (x, y) int64 200B 50 51 52 53 54 55 56 57 ... 68 69 70 71 72 73 74
        Dimensions without coordinates: x, y

Copy link
Collaborator

@ianhi ianhi left a comment

Choose a reason for hiding this comment

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

nice! thanks for this. I left one comment about the comments but otherwise seems good by me

@jsignell jsignell merged commit a96ea66 into pydata:main Jan 20, 2026
40 checks passed
@jsignell jsignell deleted the ordered-coords branch January 20, 2026 19:54
@jsignell
Copy link
Member Author

Just checked GenericMappingTools/pygmt#4143 since that was linked as fallout from the original coord ordering PR. It looks like they had a simple case (all dimension coordinates) so the repr should be the same with the PR in as it was after #10778. No change needed on that library

tonybaloney pushed a commit to tonybaloney/swe-complex-xarray that referenced this pull request Mar 19, 2026
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.

Ordering of Coords in Repr

2 participants