Skip to content

Commit a96ea66

Browse files
authored
Better ordering of coords in repr (pydata#11091)
1 parent 9fe835f commit a96ea66

9 files changed

Lines changed: 47 additions & 18 deletions

File tree

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ New Features
3636
of the same name are compared for potential conflicts when performing binary operations. The default for it is
3737
``arithmetic_compat='minimal'`` which matches the existing behaviour.
3838
By `Matthew Willson <https://github.com/mjwillson>`_.
39+
- Better ordering of coordinates when displaying Xarray objects. (:pull:`11098`).
40+
By `Ian Hunt-Isaak <https://github.com/ianhi>`_, `Julia Signell <https://github.com/jsignell>`_.
3941

4042
Breaking Changes
4143
~~~~~~~~~~~~~~~~

xarray/core/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,9 @@ def assign_coords(
619619
<xarray.Dataset> Size: 360B
620620
Dimensions: (x: 2, y: 2, time: 4)
621621
Coordinates:
622-
* time (time) datetime64[ns] 32B 2014-09-06 ... 2014-09-09
623622
lon (x, y) float64 32B 260.2 260.7 260.2 260.8
624623
lat (x, y) float64 32B 42.25 42.21 42.63 42.59
624+
* time (time) datetime64[ns] 32B 2014-09-06 ... 2014-09-09
625625
reference_time datetime64[ns] 8B 2014-09-05
626626
Dimensions without coordinates: x, y
627627
Data variables:
@@ -633,9 +633,9 @@ def assign_coords(
633633
<xarray.Dataset> Size: 360B
634634
Dimensions: (x: 2, y: 2, time: 4)
635635
Coordinates:
636-
* time (time) datetime64[ns] 32B 2014-09-06 ... 2014-09-09
637636
lon (x, y) float64 32B -99.83 -99.32 -99.79 -99.23
638637
lat (x, y) float64 32B 42.25 42.21 42.63 42.59
638+
* time (time) datetime64[ns] 32B 2014-09-06 ... 2014-09-09
639639
reference_time datetime64[ns] 8B 2014-09-05
640640
Dimensions without coordinates: x, y
641641
Data variables:

xarray/core/dataarray.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ class DataArray(
369369
[[22.60070734, 13.78914233, 14.17424919],
370370
[18.28478802, 16.15234857, 26.63418806]]])
371371
Coordinates:
372-
* time (time) datetime64[ns] 24B 2014-09-06 2014-09-07 2014-09-08
373372
lon (x, y) float64 32B -99.83 -99.32 -99.79 -99.23
374373
lat (x, y) float64 32B 42.25 42.21 42.63 42.59
374+
* time (time) datetime64[ns] 24B 2014-09-06 2014-09-07 2014-09-08
375375
reference_time datetime64[ns] 8B 2014-09-05
376376
Dimensions without coordinates: x, y
377377
Attributes:
@@ -2807,8 +2807,8 @@ def set_index(
28072807
[1., 1., 1.]])
28082808
Coordinates:
28092809
* x (x) int64 16B 0 1
2810-
* y (y) int64 24B 0 1 2
28112810
a (x) int64 16B 3 4
2811+
* y (y) int64 24B 0 1 2
28122812
>>> arr.set_index(x="a")
28132813
<xarray.DataArray (x: 2, y: 3)> Size: 48B
28142814
array([[1., 1., 1.],
@@ -5964,8 +5964,8 @@ def pad(
59645964
[nan, nan, nan, nan]])
59655965
Coordinates:
59665966
* x (x) float64 32B nan 0.0 1.0 nan
5967-
* y (y) int64 32B 10 20 30 40
59685967
z (x) float64 32B nan 100.0 200.0 nan
5968+
* y (y) int64 32B 10 20 30 40
59695969
59705970
Careful, ``constant_values`` are coerced to the data type of the array which may
59715971
lead to a loss of precision:
@@ -5978,8 +5978,8 @@ def pad(
59785978
[ 1, 1, 1, 1]])
59795979
Coordinates:
59805980
* x (x) float64 32B nan 0.0 1.0 nan
5981-
* y (y) int64 32B 10 20 30 40
59825981
z (x) float64 32B nan 100.0 200.0 nan
5982+
* y (y) int64 32B 10 20 30 40
59835983
"""
59845984
ds = self._to_temp_dataset().pad(
59855985
pad_width=pad_width,

xarray/core/dataset.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ class Dataset(
322322
<xarray.Dataset> Size: 552B
323323
Dimensions: (loc: 2, instrument: 3, time: 4)
324324
Coordinates:
325-
* instrument (instrument) <U8 96B 'manufac1' 'manufac2' 'manufac3'
326-
* time (time) datetime64[ns] 32B 2014-09-06 ... 2014-09-09
327325
lon (loc) float64 16B -99.83 -99.32
328326
lat (loc) float64 16B 42.25 42.21
327+
* instrument (instrument) <U8 96B 'manufac1' 'manufac2' 'manufac3'
328+
* time (time) datetime64[ns] 32B 2014-09-06 ... 2014-09-09
329329
reference_time datetime64[ns] 8B 2014-09-05
330330
Dimensions without coordinates: loc
331331
Data variables:
@@ -4733,8 +4733,8 @@ def set_index(
47334733
Dimensions: (x: 2, y: 3)
47344734
Coordinates:
47354735
* x (x) int64 16B 0 1
4736-
* y (y) int64 24B 0 1 2
47374736
a (x) int64 16B 3 4
4737+
* y (y) int64 24B 0 1 2
47384738
Data variables:
47394739
v (x, y) float64 48B 1.0 1.0 1.0 1.0 1.0 1.0
47404740
>>> ds.set_index(x="a")
@@ -8713,9 +8713,9 @@ def filter_by_attrs(self, **kwargs) -> Self:
87138713
<xarray.Dataset> Size: 192B
87148714
Dimensions: (x: 2, y: 2, time: 3)
87158715
Coordinates:
8716-
* time (time) datetime64[ns] 24B 2014-09-06 2014-09-07 2014-09-08
87178716
lon (x, y) float64 32B -99.83 -99.32 -99.79 -99.23
87188717
lat (x, y) float64 32B 42.25 42.21 42.63 42.59
8718+
* time (time) datetime64[ns] 24B 2014-09-06 2014-09-07 2014-09-08
87198719
reference_time datetime64[ns] 8B 2014-09-05
87208720
Dimensions without coordinates: x, y
87218721
Data variables:
@@ -8728,9 +8728,9 @@ def filter_by_attrs(self, **kwargs) -> Self:
87288728
<xarray.Dataset> Size: 288B
87298729
Dimensions: (x: 2, y: 2, time: 3)
87308730
Coordinates:
8731-
* time (time) datetime64[ns] 24B 2014-09-06 2014-09-07 2014-09-08
87328731
lon (x, y) float64 32B -99.83 -99.32 -99.79 -99.23
87338732
lat (x, y) float64 32B 42.25 42.21 42.63 42.59
8733+
* time (time) datetime64[ns] 24B 2014-09-06 2014-09-07 2014-09-08
87348734
reference_time datetime64[ns] 8B 2014-09-05
87358735
Dimensions without coordinates: x, y
87368736
Data variables:

xarray/core/formatting.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,38 @@ def _mapping_repr(
443443
)
444444

445445

446+
def _coord_sort_key(coord, dims):
447+
"""Sort key for coordinate ordering.
448+
449+
Orders by:
450+
1. Primary: index of the matching dimension in dataset dims.
451+
2. Secondary: dimension coordinates (name == dim) come before non-dimension coordinates
452+
453+
This groups non-dimension coordinates right after their associated dimension
454+
coordinate.
455+
"""
456+
name, var = coord
457+
458+
# Dimension coordinates come first within their dim section
459+
if name in dims:
460+
return (dims.index(name), 0)
461+
462+
# Non-dimension coordinates come second within their dim section
463+
# Check the var.dims list in backwards order to put (x, y) after (x) and (y)
464+
for d in var.dims[::-1]:
465+
if d in dims:
466+
return (dims.index(d), 1)
467+
468+
# Scalar coords or coords with dims not in dataset dims go at the end
469+
return (len(dims), 1)
470+
471+
446472
def coords_repr(coords: AbstractCoordinates, col_width=None, max_rows=None):
447473
if col_width is None:
448474
col_width = _calculate_col_width(coords)
449475
dims = tuple(coords._data.dims)
450476
dim_ordered_coords = sorted(
451-
coords.items(), key=lambda x: dims.index(x[0]) if x[0] in dims else len(dims)
477+
coords.items(), key=functools.partial(_coord_sort_key, dims=dims)
452478
)
453479
return _mapping_repr(
454480
dict(dim_ordered_coords),

xarray/core/formatting_html.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import TYPE_CHECKING
1212

1313
from xarray.core.formatting import (
14+
_coord_sort_key,
1415
filter_nondefault_indexes,
1516
inherited_vars,
1617
inline_index_repr,
@@ -120,7 +121,7 @@ def summarize_coords(variables) -> str:
120121
li_items = []
121122
dims = tuple(variables._data.dims)
122123
dim_ordered_coords = sorted(
123-
variables.items(), key=lambda x: dims.index(x[0]) if x[0] in dims else len(dims)
124+
variables.items(), key=partial(_coord_sort_key, dims=dims)
124125
)
125126
for k, v in dim_ordered_coords:
126127
li_content = summarize_variable(k, v, is_index=k in variables.xindexes)

xarray/structure/concat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ def concat(
239239
array([[0, 1, 2],
240240
[3, 4, 5]])
241241
Coordinates:
242-
* y (y) int64 24B 10 20 30
243242
x (new_dim) <U1 8B 'a' 'b'
243+
* y (y) int64 24B 10 20 30
244244
Dimensions without coordinates: new_dim
245245
246246
>>> xr.concat(
@@ -253,8 +253,8 @@ def concat(
253253
[3, 4, 5]])
254254
Coordinates:
255255
* new_dim (new_dim) int64 16B -90 -100
256-
* y (y) int64 24B 10 20 30
257256
x (new_dim) <U1 8B 'a' 'b'
257+
* y (y) int64 24B 10 20 30
258258
259259
# Concatenate a scalar variable along a new dimension of the same name with and without creating a new index
260260

xarray/tests/test_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ def test_repr(self) -> None:
294294
Coordinates:
295295
* dim2 (dim2) float64 72B 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
296296
* dim3 (dim3) {data["dim3"].dtype} 40B 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'
297-
* time (time) datetime64[ns] 160B 2000-01-01 2000-01-02 ... 2000-01-20
298297
numbers (dim3) int64 80B 0 1 2 0 0 1 1 2 2 3
298+
* time (time) datetime64[ns] 160B 2000-01-01 2000-01-02 ... 2000-01-20
299299
Dimensions without coordinates: dim1
300300
Data variables:
301301
var1 (dim1, dim2) float64 576B -0.9891 -0.3678 1.288 ... -0.2116 0.364
@@ -875,8 +875,8 @@ def test_coords_properties(self) -> None:
875875
"""\
876876
Coordinates:
877877
* x (x) int64 16B -1 -2
878-
* y (y) int64 24B 0 1 2
879878
a (x) int64 16B 4 5
879+
* y (y) int64 24B 0 1 2
880880
b int64 8B -10"""
881881
)
882882
actual = repr(coords)

xarray/tests/test_datatree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ def test_properties(self) -> None:
644644
"""\
645645
Coordinates:
646646
* x (x) int64 16B -1 -2
647-
* y (y) int64 24B 0 1 2
648647
a (x) int64 16B 4 5
648+
* y (y) int64 24B 0 1 2
649649
b int64 8B -10"""
650650
)
651651
actual = repr(coords)

0 commit comments

Comments
 (0)