Skip to content

Commit

Permalink
display the indexes in the string reprs (#6795)
Browse files Browse the repository at this point in the history
* 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 <dcherian@users.noreply.github.com>
  • Loading branch information
keewis and dcherian authored Oct 12, 2022
1 parent 96db9f8 commit b80d973
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2977,7 +2977,7 @@ def reindex(
pressure (station) float64 211.8 322.9 218.8 445.9
>>> x.indexes
Indexes:
station: Index(['boston', 'nyc', 'seattle', 'denver'], dtype='object', name='station')
station Index(['boston', 'nyc', 'seattle', 'denver'], dtype='object', name='station')
Create a new index and reindex the dataset. By default values in the new index that
do not have corresponding records in the dataset are assigned `NaN`.
Expand Down
58 changes: 50 additions & 8 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,33 @@ def coords_repr(coords, col_width=None, max_rows=None):
)


def indexes_repr(indexes):
summary = ["Indexes:"]
if indexes:
for k, v in indexes.items():
summary.append(wrap_indent(repr(v), f"{k}: "))
else:
summary += [EMPTY_REPR]
return "\n".join(summary)
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 nondefault_indexes(indexes):
from .indexes import PandasIndex, PandasMultiIndex

default_indexes = (PandasIndex, PandasMultiIndex)

return {
key: index
for key, index in indexes.items()
if not isinstance(index, default_indexes)
}


def indexes_repr(indexes, col_width=None, max_rows=None):
return _mapping_repr(
indexes,
"Indexes",
summarize_index,
"display_expand_indexes",
col_width=col_width,
max_rows=max_rows,
)


def dim_summary(obj):
Expand Down Expand Up @@ -592,6 +611,19 @@ def array_repr(arr):
if unindexed_dims_str:
summary.append(unindexed_dims_str)

display_default_indexes = _get_boolean_with_default(
"display_default_indexes", False
)
if display_default_indexes:
xindexes = arr.xindexes
else:
xindexes = nondefault_indexes(arr.xindexes)

if xindexes:
summary.append(
indexes_repr(xindexes, col_width=col_width, max_rows=max_rows)
)

if arr.attrs:
summary.append(attrs_repr(arr.attrs, max_rows=max_rows))

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

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

display_default_indexes = _get_boolean_with_default(
"display_default_indexes", False
)
if display_default_indexes:
xindexes = ds.xindexes
else:
xindexes = nondefault_indexes(ds.xindexes)
if xindexes:
summary.append(indexes_repr(xindexes, col_width=col_width, max_rows=max_rows))

if ds.attrs:
summary.append(attrs_repr(ds.attrs, max_rows=max_rows))

Expand Down
3 changes: 3 additions & 0 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@ def copy(self, deep=True):
def __getitem__(self, indexer: Any):
return self._replace(self.index[indexer])

def __repr__(self):
return f"PandasIndex({repr(self.index)})"


def _check_dim_compat(variables: Mapping[Any, Variable], all_dims: str = "equal"):
"""Check that all multi-index variable candidates are 1-dimensional and
Expand Down
8 changes: 8 additions & 0 deletions xarray/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"display_expand_coords",
"display_expand_data_vars",
"display_expand_data",
"display_expand_indexes",
"display_default_indexes",
"enable_cftimeindex",
"file_cache_maxsize",
"keep_attrs",
Expand All @@ -42,6 +44,8 @@ class T_Options(TypedDict):
display_expand_coords: Literal["default", True, False]
display_expand_data_vars: Literal["default", True, False]
display_expand_data: Literal["default", True, False]
display_expand_indexes: Literal["default", True, False]
display_default_indexes: Literal["default", True, False]
enable_cftimeindex: bool
file_cache_maxsize: int
keep_attrs: Literal["default", True, False]
Expand All @@ -62,6 +66,8 @@ class T_Options(TypedDict):
"display_expand_coords": "default",
"display_expand_data_vars": "default",
"display_expand_data": "default",
"display_expand_indexes": "default",
"display_default_indexes": False,
"enable_cftimeindex": True,
"file_cache_maxsize": 128,
"keep_attrs": "default",
Expand All @@ -88,6 +94,8 @@ def _positive_integer(value: int) -> bool:
"display_expand_coords": lambda choice: choice in [True, False, "default"],
"display_expand_data_vars": lambda choice: choice in [True, False, "default"],
"display_expand_data": lambda choice: choice in [True, False, "default"],
"display_expand_indexes": lambda choice: choice in [True, False, "default"],
"display_default_indexes": lambda choice: choice in [True, False, "default"],
"enable_cftimeindex": lambda value: isinstance(value, bool),
"file_cache_maxsize": _positive_integer,
"keep_attrs": lambda choice: choice in [True, False, "default"],
Expand Down

0 comments on commit b80d973

Please sign in to comment.