Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid converting custom indexes to pandas indexes when formatting coordinate diffs #9157

Merged
merged 6 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Deprecations

Bug fixes
~~~~~~~~~

- Don't convert custom indexes to ``pandas`` indexes when computing a diff (:pull:`9157`)
By `Justus Magin <https://github.com/keewis>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,8 +871,8 @@ def diff_coords_repr(a, b, compat, col_width=None):
"Coordinates",
summarize_variable,
col_width=col_width,
a_indexes=a.indexes,
b_indexes=b.indexes,
a_indexes=a.xindexes,
b_indexes=b.xindexes,
)


Expand Down
76 changes: 56 additions & 20 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@
import xarray as xr
from xarray.core import formatting
from xarray.core.datatree import DataTree # TODO: Remove when can do xr.DataTree
from xarray.core.indexes import Index
from xarray.tests import requires_cftime, requires_dask, requires_netCDF4


class CustomIndex(Index):
names: tuple[str, ...]

def __init__(self, names: tuple[str, ...]):
self.names = names

def __repr__(self):
return f"CustomIndex(coords={self.names})"


class TestFormatting:
def test_get_indexer_at_least_n_items(self) -> None:
cases = [
Expand Down Expand Up @@ -219,17 +230,6 @@ def test_attribute_repr(self) -> None:
assert "\t" not in tabs

def test_index_repr(self) -> None:
from xarray.core.indexes import Index

class CustomIndex(Index):
names: tuple[str, ...]

def __init__(self, names: tuple[str, ...]):
self.names = names

def __repr__(self):
return f"CustomIndex(coords={self.names})"

coord_names = ("x", "y")
index = CustomIndex(coord_names)
names = ("x",)
Expand Down Expand Up @@ -258,15 +258,6 @@ def _repr_inline_(self, max_width: int):
),
)
def test_index_repr_grouping(self, names) -> None:
from xarray.core.indexes import Index

class CustomIndex(Index):
def __init__(self, names):
self.names = names

def __repr__(self):
return f"CustomIndex(coords={self.names})"

index = CustomIndex(names)

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

da_a = xr.DataArray(
np.array([[1, 2, 3], [4, 5, 6]], dtype="int8"),
dims=("x", "y"),
coords=xr.Coordinates(
{
"x": np.array([True, False], dtype="bool"),
"y": np.array([1, 2, 3], dtype="int16"),
},
indexes={"y": CustomIndex(("y",))},
),
)

da_b = xr.DataArray(
np.array([1, 2], dtype="int8"),
dims="x",
coords=xr.Coordinates(
{
"x": np.array([True, False], dtype="bool"),
"label": ("x", np.array([1, 2], dtype="int16")),
},
indexes={"label": CustomIndex(("label",))},
),
)

expected = dedent(
"""\
Left and right DataArray objects are not equal
Differing dimensions:
(x: 2, y: 3) != (x: 2)
Differing values:
L
array([[1, 2, 3],
[4, 5, 6]], dtype=int8)
R
array([1, 2], dtype=int8)
Coordinates only on the left object:
* y (y) int16 6B 1 2 3
Coordinates only on the right object:
* label (x) int16 4B 1 2
""".rstrip()
)

actual = formatting.diff_array_repr(da_a, da_b, "equals")
assert actual == expected

va = xr.Variable(
"x", np.array([1, 2, 3], dtype="int64"), {"title": "test Variable"}
)
Expand Down
Loading