Skip to content

Commit b80d973

Browse files
keewisdcherian
andauthored
display the indexes in the string reprs (#6795)
* add indexes to the string repr * switch indexes_repr to use _mapping_repr * only print the indexes if there are any * make use of col_width and max_rows * display the wrapped index to get rid of the test collection errors * skip default indexes by default * update the doctest for indexes Co-authored-by: Deepak Cherian <[email protected]>
1 parent 96db9f8 commit b80d973

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

xarray/core/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2977,7 +2977,7 @@ def reindex(
29772977
pressure (station) float64 211.8 322.9 218.8 445.9
29782978
>>> x.indexes
29792979
Indexes:
2980-
station: Index(['boston', 'nyc', 'seattle', 'denver'], dtype='object', name='station')
2980+
station Index(['boston', 'nyc', 'seattle', 'denver'], dtype='object', name='station')
29812981
29822982
Create a new index and reindex the dataset. By default values in the new index that
29832983
do not have corresponding records in the dataset are assigned `NaN`.

xarray/core/formatting.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,33 @@ def coords_repr(coords, col_width=None, max_rows=None):
408408
)
409409

410410

411-
def indexes_repr(indexes):
412-
summary = ["Indexes:"]
413-
if indexes:
414-
for k, v in indexes.items():
415-
summary.append(wrap_indent(repr(v), f"{k}: "))
416-
else:
417-
summary += [EMPTY_REPR]
418-
return "\n".join(summary)
411+
def summarize_index(
412+
name: Hashable, index, col_width: int, max_width: int = None, is_index: bool = False
413+
):
414+
return pretty_print(f" {name} ", col_width) + f"{repr(index)}"
415+
416+
417+
def nondefault_indexes(indexes):
418+
from .indexes import PandasIndex, PandasMultiIndex
419+
420+
default_indexes = (PandasIndex, PandasMultiIndex)
421+
422+
return {
423+
key: index
424+
for key, index in indexes.items()
425+
if not isinstance(index, default_indexes)
426+
}
427+
428+
429+
def indexes_repr(indexes, col_width=None, max_rows=None):
430+
return _mapping_repr(
431+
indexes,
432+
"Indexes",
433+
summarize_index,
434+
"display_expand_indexes",
435+
col_width=col_width,
436+
max_rows=max_rows,
437+
)
419438

420439

421440
def dim_summary(obj):
@@ -592,6 +611,19 @@ def array_repr(arr):
592611
if unindexed_dims_str:
593612
summary.append(unindexed_dims_str)
594613

614+
display_default_indexes = _get_boolean_with_default(
615+
"display_default_indexes", False
616+
)
617+
if display_default_indexes:
618+
xindexes = arr.xindexes
619+
else:
620+
xindexes = nondefault_indexes(arr.xindexes)
621+
622+
if xindexes:
623+
summary.append(
624+
indexes_repr(xindexes, col_width=col_width, max_rows=max_rows)
625+
)
626+
595627
if arr.attrs:
596628
summary.append(attrs_repr(arr.attrs, max_rows=max_rows))
597629

@@ -618,6 +650,16 @@ def dataset_repr(ds):
618650

619651
summary.append(data_vars_repr(ds.data_vars, col_width=col_width, max_rows=max_rows))
620652

653+
display_default_indexes = _get_boolean_with_default(
654+
"display_default_indexes", False
655+
)
656+
if display_default_indexes:
657+
xindexes = ds.xindexes
658+
else:
659+
xindexes = nondefault_indexes(ds.xindexes)
660+
if xindexes:
661+
summary.append(indexes_repr(xindexes, col_width=col_width, max_rows=max_rows))
662+
621663
if ds.attrs:
622664
summary.append(attrs_repr(ds.attrs, max_rows=max_rows))
623665

xarray/core/indexes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ def copy(self, deep=True):
540540
def __getitem__(self, indexer: Any):
541541
return self._replace(self.index[indexer])
542542

543+
def __repr__(self):
544+
return f"PandasIndex({repr(self.index)})"
545+
543546

544547
def _check_dim_compat(variables: Mapping[Any, Variable], all_dims: str = "equal"):
545548
"""Check that all multi-index variable candidates are 1-dimensional and

xarray/core/options.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"display_expand_coords",
2323
"display_expand_data_vars",
2424
"display_expand_data",
25+
"display_expand_indexes",
26+
"display_default_indexes",
2527
"enable_cftimeindex",
2628
"file_cache_maxsize",
2729
"keep_attrs",
@@ -42,6 +44,8 @@ class T_Options(TypedDict):
4244
display_expand_coords: Literal["default", True, False]
4345
display_expand_data_vars: Literal["default", True, False]
4446
display_expand_data: Literal["default", True, False]
47+
display_expand_indexes: Literal["default", True, False]
48+
display_default_indexes: Literal["default", True, False]
4549
enable_cftimeindex: bool
4650
file_cache_maxsize: int
4751
keep_attrs: Literal["default", True, False]
@@ -62,6 +66,8 @@ class T_Options(TypedDict):
6266
"display_expand_coords": "default",
6367
"display_expand_data_vars": "default",
6468
"display_expand_data": "default",
69+
"display_expand_indexes": "default",
70+
"display_default_indexes": False,
6571
"enable_cftimeindex": True,
6672
"file_cache_maxsize": 128,
6773
"keep_attrs": "default",
@@ -88,6 +94,8 @@ def _positive_integer(value: int) -> bool:
8894
"display_expand_coords": lambda choice: choice in [True, False, "default"],
8995
"display_expand_data_vars": lambda choice: choice in [True, False, "default"],
9096
"display_expand_data": lambda choice: choice in [True, False, "default"],
97+
"display_expand_indexes": lambda choice: choice in [True, False, "default"],
98+
"display_default_indexes": lambda choice: choice in [True, False, "default"],
9199
"enable_cftimeindex": lambda value: isinstance(value, bool),
92100
"file_cache_maxsize": _positive_integer,
93101
"keep_attrs": lambda choice: choice in [True, False, "default"],

0 commit comments

Comments
 (0)