1
1
import re
2
+ import sys
2
3
import typing
3
4
from copy import copy , deepcopy
4
5
from textwrap import dedent
15
16
from xarray .testing import assert_equal , assert_identical
16
17
from xarray .tests import assert_array_equal , create_test_data , source_ndarray
17
18
19
+ ON_WINDOWS = sys .platform == "win32"
20
+
18
21
19
22
class TestTreeCreation :
20
23
def test_empty (self ):
@@ -1052,7 +1055,7 @@ def test_repr_two_children(self):
1052
1055
{
1053
1056
"/" : Dataset (coords = {"x" : [1.0 ]}),
1054
1057
"/first_child" : None ,
1055
- "/second_child" : Dataset ({"foo" : ("x" , [0.0 ])}),
1058
+ "/second_child" : Dataset ({"foo" : ("x" , [0.0 ])}, coords = { "z" : 1.0 } ),
1056
1059
}
1057
1060
)
1058
1061
@@ -1067,6 +1070,8 @@ def test_repr_two_children(self):
1067
1070
├── Group: /first_child
1068
1071
└── Group: /second_child
1069
1072
Dimensions: (x: 1)
1073
+ Coordinates:
1074
+ z float64 8B 1.0
1070
1075
Data variables:
1071
1076
foo (x) float64 8B 0.0
1072
1077
"""
@@ -1091,6 +1096,8 @@ def test_repr_two_children(self):
1091
1096
<xarray.DataTree 'second_child'>
1092
1097
Group: /second_child
1093
1098
Dimensions: (x: 1)
1099
+ Coordinates:
1100
+ z float64 8B 1.0
1094
1101
Inherited coordinates:
1095
1102
* x (x) float64 8B 1.0
1096
1103
Data variables:
@@ -1138,6 +1145,104 @@ def test_repr_inherited_dims(self):
1138
1145
).strip ()
1139
1146
assert result == expected
1140
1147
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
+
1141
1246
1142
1247
def _exact_match (message : str ) -> str :
1143
1248
return re .escape (dedent (message ).strip ())
0 commit comments