|
10 | 10 | import xarray as xr
|
11 | 11 | from xarray.core import formatting
|
12 | 12 | from xarray.core.datatree import DataTree # TODO: Remove when can do xr.DataTree
|
| 13 | +from xarray.core.indexes import Index |
13 | 14 | from xarray.tests import requires_cftime, requires_dask, requires_netCDF4
|
14 | 15 |
|
15 | 16 |
|
| 17 | +class CustomIndex(Index): |
| 18 | + names: tuple[str, ...] |
| 19 | + |
| 20 | + def __init__(self, names: tuple[str, ...]): |
| 21 | + self.names = names |
| 22 | + |
| 23 | + def __repr__(self): |
| 24 | + return f"CustomIndex(coords={self.names})" |
| 25 | + |
| 26 | + |
16 | 27 | class TestFormatting:
|
17 | 28 | def test_get_indexer_at_least_n_items(self) -> None:
|
18 | 29 | cases = [
|
@@ -219,17 +230,6 @@ def test_attribute_repr(self) -> None:
|
219 | 230 | assert "\t" not in tabs
|
220 | 231 |
|
221 | 232 | def test_index_repr(self) -> None:
|
222 |
| - from xarray.core.indexes import Index |
223 |
| - |
224 |
| - class CustomIndex(Index): |
225 |
| - names: tuple[str, ...] |
226 |
| - |
227 |
| - def __init__(self, names: tuple[str, ...]): |
228 |
| - self.names = names |
229 |
| - |
230 |
| - def __repr__(self): |
231 |
| - return f"CustomIndex(coords={self.names})" |
232 |
| - |
233 | 233 | coord_names = ("x", "y")
|
234 | 234 | index = CustomIndex(coord_names)
|
235 | 235 | names = ("x",)
|
@@ -258,15 +258,6 @@ def _repr_inline_(self, max_width: int):
|
258 | 258 | ),
|
259 | 259 | )
|
260 | 260 | def test_index_repr_grouping(self, names) -> None:
|
261 |
| - from xarray.core.indexes import Index |
262 |
| - |
263 |
| - class CustomIndex(Index): |
264 |
| - def __init__(self, names): |
265 |
| - self.names = names |
266 |
| - |
267 |
| - def __repr__(self): |
268 |
| - return f"CustomIndex(coords={self.names})" |
269 |
| - |
270 | 261 | index = CustomIndex(names)
|
271 | 262 |
|
272 | 263 | normal = formatting.summarize_index(names, index, col_width=20)
|
@@ -337,6 +328,51 @@ def test_diff_array_repr(self) -> None:
|
337 | 328 | # depending on platform, dtype may not be shown in numpy array repr
|
338 | 329 | assert actual == expected.replace(", dtype=int64", "")
|
339 | 330 |
|
| 331 | + da_a = xr.DataArray( |
| 332 | + np.array([[1, 2, 3], [4, 5, 6]], dtype="int8"), |
| 333 | + dims=("x", "y"), |
| 334 | + coords=xr.Coordinates( |
| 335 | + { |
| 336 | + "x": np.array([True, False], dtype="bool"), |
| 337 | + "y": np.array([1, 2, 3], dtype="int16"), |
| 338 | + }, |
| 339 | + indexes={"y": CustomIndex(("y",))}, |
| 340 | + ), |
| 341 | + ) |
| 342 | + |
| 343 | + da_b = xr.DataArray( |
| 344 | + np.array([1, 2], dtype="int8"), |
| 345 | + dims="x", |
| 346 | + coords=xr.Coordinates( |
| 347 | + { |
| 348 | + "x": np.array([True, False], dtype="bool"), |
| 349 | + "label": ("x", np.array([1, 2], dtype="int16")), |
| 350 | + }, |
| 351 | + indexes={"label": CustomIndex(("label",))}, |
| 352 | + ), |
| 353 | + ) |
| 354 | + |
| 355 | + expected = dedent( |
| 356 | + """\ |
| 357 | + Left and right DataArray objects are not equal |
| 358 | + Differing dimensions: |
| 359 | + (x: 2, y: 3) != (x: 2) |
| 360 | + Differing values: |
| 361 | + L |
| 362 | + array([[1, 2, 3], |
| 363 | + [4, 5, 6]], dtype=int8) |
| 364 | + R |
| 365 | + array([1, 2], dtype=int8) |
| 366 | + Coordinates only on the left object: |
| 367 | + * y (y) int16 6B 1 2 3 |
| 368 | + Coordinates only on the right object: |
| 369 | + * label (x) int16 4B 1 2 |
| 370 | + """.rstrip() |
| 371 | + ) |
| 372 | + |
| 373 | + actual = formatting.diff_array_repr(da_a, da_b, "equals") |
| 374 | + assert actual == expected |
| 375 | + |
340 | 376 | va = xr.Variable(
|
341 | 377 | "x", np.array([1, 2, 3], dtype="int64"), {"title": "test Variable"}
|
342 | 378 | )
|
|
0 commit comments