Skip to content

Commit 52a7371

Browse files
keewisdcherian
andauthored
avoid converting custom indexes to pandas indexes when formatting coordinate diffs (#9157)
* use `.xindexes` to construct the diff * check that the indexes are never converted to pandas indexes * deduplicate the various custom index dummy implementations * whats-new [skip-rtd] * fill in the pr number --------- Co-authored-by: Deepak Cherian <[email protected]>
1 parent a86c3ff commit 52a7371

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Deprecations
3737

3838
Bug fixes
3939
~~~~~~~~~
40+
- Don't convert custom indexes to ``pandas`` indexes when computing a diff (:pull:`9157`)
41+
By `Justus Magin <https://github.com/keewis>`_.
4042
- Make :py:func:`testing.assert_allclose` work with numpy 2.0 (:issue:`9165`, :pull:`9166`).
4143
By `Pontus Lurcock <https://github.com/pont-us>`_.
4244
- Allow diffing objects with array attributes on variables (:issue:`9153`, :pull:`9169`).

xarray/core/formatting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,8 @@ def diff_coords_repr(a, b, compat, col_width=None):
898898
"Coordinates",
899899
summarize_variable,
900900
col_width=col_width,
901-
a_indexes=a.indexes,
902-
b_indexes=b.indexes,
901+
a_indexes=a.xindexes,
902+
b_indexes=b.xindexes,
903903
)
904904

905905

xarray/tests/test_formatting.py

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@
1010
import xarray as xr
1111
from xarray.core import formatting
1212
from xarray.core.datatree import DataTree # TODO: Remove when can do xr.DataTree
13+
from xarray.core.indexes import Index
1314
from xarray.tests import requires_cftime, requires_dask, requires_netCDF4
1415

1516

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+
1627
class TestFormatting:
1728
def test_get_indexer_at_least_n_items(self) -> None:
1829
cases = [
@@ -219,17 +230,6 @@ def test_attribute_repr(self) -> None:
219230
assert "\t" not in tabs
220231

221232
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-
233233
coord_names = ("x", "y")
234234
index = CustomIndex(coord_names)
235235
names = ("x",)
@@ -258,15 +258,6 @@ def _repr_inline_(self, max_width: int):
258258
),
259259
)
260260
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-
270261
index = CustomIndex(names)
271262

272263
normal = formatting.summarize_index(names, index, col_width=20)
@@ -337,6 +328,51 @@ def test_diff_array_repr(self) -> None:
337328
# depending on platform, dtype may not be shown in numpy array repr
338329
assert actual == expected.replace(", dtype=int64", "")
339330

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+
340376
va = xr.Variable(
341377
"x", np.array([1, 2, 3], dtype="int64"), {"title": "test Variable"}
342378
)

0 commit comments

Comments
 (0)