Skip to content

Commit ab84e04

Browse files
authored
Fix DataTree repr to not repeat inherited coordinates (#9532)
* Fix DataTree repr to not repeat inherited coordinates Fixes GH9499 * skip failing test on Windows
1 parent 2a6212e commit ab84e04

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

xarray/core/formatting.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,8 @@ def _datatree_node_repr(node: DataTree, show_inherited: bool) -> str:
11021102
summary.append(f"{dims_start}({dims_values})")
11031103

11041104
if node._node_coord_variables:
1105-
summary.append(coords_repr(node.coords, col_width=col_width, max_rows=max_rows))
1105+
node_coords = node.to_dataset(inherited=False).coords
1106+
summary.append(coords_repr(node_coords, col_width=col_width, max_rows=max_rows))
11061107

11071108
if show_inherited and inherited_coords:
11081109
summary.append(

xarray/tests/test_datatree.py

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import sys
23
import typing
34
from copy import copy, deepcopy
45
from textwrap import dedent
@@ -15,6 +16,8 @@
1516
from xarray.testing import assert_equal, assert_identical
1617
from xarray.tests import assert_array_equal, create_test_data, source_ndarray
1718

19+
ON_WINDOWS = sys.platform == "win32"
20+
1821

1922
class TestTreeCreation:
2023
def test_empty(self):
@@ -1052,7 +1055,7 @@ def test_repr_two_children(self):
10521055
{
10531056
"/": Dataset(coords={"x": [1.0]}),
10541057
"/first_child": None,
1055-
"/second_child": Dataset({"foo": ("x", [0.0])}),
1058+
"/second_child": Dataset({"foo": ("x", [0.0])}, coords={"z": 1.0}),
10561059
}
10571060
)
10581061

@@ -1067,6 +1070,8 @@ def test_repr_two_children(self):
10671070
├── Group: /first_child
10681071
└── Group: /second_child
10691072
Dimensions: (x: 1)
1073+
Coordinates:
1074+
z float64 8B 1.0
10701075
Data variables:
10711076
foo (x) float64 8B 0.0
10721077
"""
@@ -1091,6 +1096,8 @@ def test_repr_two_children(self):
10911096
<xarray.DataTree 'second_child'>
10921097
Group: /second_child
10931098
Dimensions: (x: 1)
1099+
Coordinates:
1100+
z float64 8B 1.0
10941101
Inherited coordinates:
10951102
* x (x) float64 8B 1.0
10961103
Data variables:
@@ -1138,6 +1145,104 @@ def test_repr_inherited_dims(self):
11381145
).strip()
11391146
assert result == expected
11401147

1148+
@pytest.mark.skipif(
1149+
ON_WINDOWS, reason="windows (pre NumPy2) uses int32 instead of int64"
1150+
)
1151+
def test_doc_example(self):
1152+
# regression test for https://github.com/pydata/xarray/issues/9499
1153+
time = xr.DataArray(data=["2022-01", "2023-01"], dims="time")
1154+
stations = xr.DataArray(data=list("abcdef"), dims="station")
1155+
lon = [-100, -80, -60]
1156+
lat = [10, 20, 30]
1157+
# Set up fake data
1158+
wind_speed = xr.DataArray(np.ones((2, 6)) * 2, dims=("time", "station"))
1159+
pressure = xr.DataArray(np.ones((2, 6)) * 3, dims=("time", "station"))
1160+
air_temperature = xr.DataArray(np.ones((2, 6)) * 4, dims=("time", "station"))
1161+
dewpoint = xr.DataArray(np.ones((2, 6)) * 5, dims=("time", "station"))
1162+
infrared = xr.DataArray(np.ones((2, 3, 3)) * 6, dims=("time", "lon", "lat"))
1163+
true_color = xr.DataArray(np.ones((2, 3, 3)) * 7, dims=("time", "lon", "lat"))
1164+
tree = xr.DataTree.from_dict(
1165+
{
1166+
"/": xr.Dataset(
1167+
coords={"time": time},
1168+
),
1169+
"/weather": xr.Dataset(
1170+
coords={"station": stations},
1171+
data_vars={
1172+
"wind_speed": wind_speed,
1173+
"pressure": pressure,
1174+
},
1175+
),
1176+
"/weather/temperature": xr.Dataset(
1177+
data_vars={
1178+
"air_temperature": air_temperature,
1179+
"dewpoint": dewpoint,
1180+
},
1181+
),
1182+
"/satellite": xr.Dataset(
1183+
coords={"lat": lat, "lon": lon},
1184+
data_vars={
1185+
"infrared": infrared,
1186+
"true_color": true_color,
1187+
},
1188+
),
1189+
},
1190+
)
1191+
1192+
result = repr(tree)
1193+
expected = dedent(
1194+
"""
1195+
<xarray.DataTree>
1196+
Group: /
1197+
│ Dimensions: (time: 2)
1198+
│ Coordinates:
1199+
│ * time (time) <U7 56B '2022-01' '2023-01'
1200+
├── Group: /weather
1201+
│ │ Dimensions: (station: 6, time: 2)
1202+
│ │ Coordinates:
1203+
│ │ * station (station) <U1 24B 'a' 'b' 'c' 'd' 'e' 'f'
1204+
│ │ Data variables:
1205+
│ │ wind_speed (time, station) float64 96B 2.0 2.0 2.0 2.0 ... 2.0 2.0 2.0 2.0
1206+
│ │ pressure (time, station) float64 96B 3.0 3.0 3.0 3.0 ... 3.0 3.0 3.0 3.0
1207+
│ └── Group: /weather/temperature
1208+
│ Dimensions: (time: 2, station: 6)
1209+
│ Data variables:
1210+
│ air_temperature (time, station) float64 96B 4.0 4.0 4.0 4.0 ... 4.0 4.0 4.0
1211+
│ dewpoint (time, station) float64 96B 5.0 5.0 5.0 5.0 ... 5.0 5.0 5.0
1212+
└── Group: /satellite
1213+
Dimensions: (lat: 3, lon: 3, time: 2)
1214+
Coordinates:
1215+
* lat (lat) int64 24B 10 20 30
1216+
* lon (lon) int64 24B -100 -80 -60
1217+
Data variables:
1218+
infrared (time, lon, lat) float64 144B 6.0 6.0 6.0 6.0 ... 6.0 6.0 6.0
1219+
true_color (time, lon, lat) float64 144B 7.0 7.0 7.0 7.0 ... 7.0 7.0 7.0
1220+
"""
1221+
).strip()
1222+
assert result == expected
1223+
1224+
result = repr(tree["weather"])
1225+
expected = dedent(
1226+
"""
1227+
<xarray.DataTree 'weather'>
1228+
Group: /weather
1229+
│ Dimensions: (time: 2, station: 6)
1230+
│ Coordinates:
1231+
│ * station (station) <U1 24B 'a' 'b' 'c' 'd' 'e' 'f'
1232+
│ Inherited coordinates:
1233+
│ * time (time) <U7 56B '2022-01' '2023-01'
1234+
│ Data variables:
1235+
│ wind_speed (time, station) float64 96B 2.0 2.0 2.0 2.0 ... 2.0 2.0 2.0 2.0
1236+
│ pressure (time, station) float64 96B 3.0 3.0 3.0 3.0 ... 3.0 3.0 3.0 3.0
1237+
└── Group: /weather/temperature
1238+
Dimensions: (time: 2, station: 6)
1239+
Data variables:
1240+
air_temperature (time, station) float64 96B 4.0 4.0 4.0 4.0 ... 4.0 4.0 4.0
1241+
dewpoint (time, station) float64 96B 5.0 5.0 5.0 5.0 ... 5.0 5.0 5.0
1242+
"""
1243+
).strip()
1244+
assert result == expected
1245+
11411246

11421247
def _exact_match(message: str) -> str:
11431248
return re.escape(dedent(message).strip())

0 commit comments

Comments
 (0)