Skip to content

use _repr_inline_ for indexes that define it #7183

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

Merged
merged 17 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 20 additions & 4 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,26 @@ def coords_repr(coords, col_width=None, max_rows=None):
)


def summarize_index(
name: Hashable, index, col_width: int, max_width: int = None, is_index: bool = False
):
return pretty_print(f" {name} ", col_width) + f"{repr(index)}"
def inline_index_repr(index, max_width=None):
if hasattr(index, "_repr_inline_"):
repr_ = index._repr_inline_(max_width=max_width)
else:
# fallback for the `pandas.Index` subclasses from
# `Indexes.get_pandas_indexes` / `xr_obj.indexes`
repr_ = repr(index)

return repr_


def summarize_index(name: Hashable, index, col_width: int, max_width: int = None):
if max_width is None:
max_width = OPTIONS["display_width"]

preformatted = pretty_print(f" {name} ", col_width)

index_width = max_width - len(preformatted)
repr_ = inline_index_repr(index, max_width=index_width)
return preformatted + repr_


def nondefault_indexes(indexes):
Expand Down
3 changes: 3 additions & 0 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def _copy(self, deep: bool = True, memo: dict[int, Any] | None = None) -> Index:
def __getitem__(self, indexer: Any):
raise NotImplementedError()

def _repr_inline_(self, max_width):
return self.__class__.__name__


def _maybe_cast_to_cftimeindex(index: pd.Index) -> pd.Index:
from ..coding.cftimeindex import CFTimeIndex
Expand Down
25 changes: 25 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,31 @@ def test_attribute_repr(self) -> None:
assert "\n" not in newlines
assert "\t" not in tabs

def test_index_repr(self):
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})"

coord_names = ["x", "y"]
index = CustomIndex(coord_names)
name = "x"

normal = formatting.summarize_index(name, index, col_width=20)
assert name in normal
assert "CustomIndex" in normal

CustomIndex._repr_inline_ = (
lambda self, max_width: f"CustomIndex[{', '.join(self.names)}]"
)
inline = formatting.summarize_index(name, index, col_width=20)
assert name in inline
assert index._repr_inline_(max_width=40) in inline

def test_diff_array_repr(self) -> None:
da_a = xr.DataArray(
np.array([[1, 2, 3], [4, 5, 6]], dtype="int64"),
Expand Down