From e2d80124f74234ff689bc644392df5a7459d7bf6 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sat, 16 Jul 2022 20:49:35 +0200 Subject: [PATCH 1/7] add indexes to the string repr --- xarray/core/formatting.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index be5e06becdf..b6b9b74f197 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -591,6 +591,8 @@ def array_repr(arr): if unindexed_dims_str: summary.append(unindexed_dims_str) + summary.append(indexes_repr(arr.xindexes)) + if arr.attrs: summary.append(attrs_repr(arr.attrs)) @@ -615,6 +617,7 @@ def dataset_repr(ds): summary.append(unindexed_dims_str) summary.append(data_vars_repr(ds.data_vars, col_width=col_width, max_rows=max_rows)) + summary.append(indexes_repr(ds.xindexes)) if ds.attrs: summary.append(attrs_repr(ds.attrs, max_rows=max_rows)) From 18d5ac707c5c64aa5b2e0b703e17a6821c0b640d Mon Sep 17 00:00:00 2001 From: Keewis Date: Sat, 16 Jul 2022 21:10:52 +0200 Subject: [PATCH 2/7] switch indexes_repr to use _mapping_repr --- xarray/core/formatting.py | 19 ++++++++++++------- xarray/core/options.py | 4 ++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index b6b9b74f197..d69446c9c64 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -408,14 +408,19 @@ 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 f" {name}: {repr(index)}" + + 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) + return _mapping_repr( + indexes, + "Indexes", + summarize_index, + "display_expand_indexes", + ) def dim_summary(obj): diff --git a/xarray/core/options.py b/xarray/core/options.py index 80c8d0c7c97..a96cce08b24 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -22,6 +22,7 @@ "display_expand_coords", "display_expand_data_vars", "display_expand_data", + "display_expand_indexes", "enable_cftimeindex", "file_cache_maxsize", "keep_attrs", @@ -42,6 +43,7 @@ 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] enable_cftimeindex: bool file_cache_maxsize: int keep_attrs: Literal["default", True, False] @@ -62,6 +64,7 @@ class T_Options(TypedDict): "display_expand_coords": "default", "display_expand_data_vars": "default", "display_expand_data": "default", + "display_expand_indexes": "default", "enable_cftimeindex": True, "file_cache_maxsize": 128, "keep_attrs": "default", @@ -88,6 +91,7 @@ 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"], "enable_cftimeindex": lambda value: isinstance(value, bool), "file_cache_maxsize": _positive_integer, "keep_attrs": lambda choice: choice in [True, False, "default"], From 969ac2e7b5084eb5ab17173f284d69e216e8043a Mon Sep 17 00:00:00 2001 From: Keewis Date: Sat, 16 Jul 2022 21:12:25 +0200 Subject: [PATCH 3/7] only print the indexes if there are any --- xarray/core/formatting.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index d69446c9c64..cf86a4f7e41 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -596,7 +596,8 @@ def array_repr(arr): if unindexed_dims_str: summary.append(unindexed_dims_str) - summary.append(indexes_repr(arr.xindexes)) + if arr.xindexes: + summary.append(indexes_repr(arr.xindexes)) if arr.attrs: summary.append(attrs_repr(arr.attrs)) @@ -622,7 +623,8 @@ def dataset_repr(ds): summary.append(unindexed_dims_str) summary.append(data_vars_repr(ds.data_vars, col_width=col_width, max_rows=max_rows)) - summary.append(indexes_repr(ds.xindexes)) + if ds.xindexes: + summary.append(indexes_repr(ds.xindexes)) if ds.attrs: summary.append(attrs_repr(ds.attrs, max_rows=max_rows)) From cf8b9e2905bde74bb1d039167e4f68fad8be8081 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sat, 16 Jul 2022 21:25:29 +0200 Subject: [PATCH 4/7] make use of col_width and max_rows --- xarray/core/formatting.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index cf86a4f7e41..4f3380084e3 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -411,15 +411,17 @@ 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 f" {name}: {repr(index)}" + return pretty_print(f" {name} ", col_width) + f"{repr(index)}" -def indexes_repr(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, ) @@ -597,7 +599,9 @@ def array_repr(arr): summary.append(unindexed_dims_str) if arr.xindexes: - summary.append(indexes_repr(arr.xindexes)) + summary.append( + indexes_repr(arr.xindexes, col_width=col_width, max_rows=max_rows) + ) if arr.attrs: summary.append(attrs_repr(arr.attrs)) @@ -624,7 +628,9 @@ def dataset_repr(ds): summary.append(data_vars_repr(ds.data_vars, col_width=col_width, max_rows=max_rows)) if ds.xindexes: - summary.append(indexes_repr(ds.xindexes)) + summary.append( + indexes_repr(ds.xindexes, col_width=col_width, max_rows=max_rows) + ) if ds.attrs: summary.append(attrs_repr(ds.attrs, max_rows=max_rows)) From b91e7867fad19c8c4cf5e7c4432360174a06ca44 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sat, 16 Jul 2022 22:16:46 +0200 Subject: [PATCH 5/7] display the wrapped index to get rid of the test collection errors --- xarray/core/indexes.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xarray/core/indexes.py b/xarray/core/indexes.py index 8589496b5eb..04475a7d9ce 100644 --- a/xarray/core/indexes.py +++ b/xarray/core/indexes.py @@ -483,6 +483,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 From 8f21df36e22d996bb436f87df1a3257da6015c47 Mon Sep 17 00:00:00 2001 From: Keewis Date: Tue, 26 Jul 2022 15:56:29 +0200 Subject: [PATCH 6/7] skip default indexes by default --- xarray/core/formatting.py | 38 ++++++++++++++++++++++++++++++++------ xarray/core/options.py | 4 ++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 4f3380084e3..5ecd18827a2 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -414,6 +414,18 @@ def summarize_index( 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, @@ -598,9 +610,17 @@ def array_repr(arr): if unindexed_dims_str: summary.append(unindexed_dims_str) - if arr.xindexes: + 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(arr.xindexes, col_width=col_width, max_rows=max_rows) + indexes_repr(xindexes, col_width=col_width, max_rows=max_rows) ) if arr.attrs: @@ -627,10 +647,16 @@ def dataset_repr(ds): summary.append(unindexed_dims_str) summary.append(data_vars_repr(ds.data_vars, col_width=col_width, max_rows=max_rows)) - if ds.xindexes: - summary.append( - indexes_repr(ds.xindexes, 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)) diff --git a/xarray/core/options.py b/xarray/core/options.py index a96cce08b24..865d0755a01 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -23,6 +23,7 @@ "display_expand_data_vars", "display_expand_data", "display_expand_indexes", + "display_default_indexes", "enable_cftimeindex", "file_cache_maxsize", "keep_attrs", @@ -44,6 +45,7 @@ class T_Options(TypedDict): 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] @@ -65,6 +67,7 @@ class T_Options(TypedDict): "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", @@ -92,6 +95,7 @@ def _positive_integer(value: int) -> bool: "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"], From 4292ab4c8a13fac2700584057f4f3c7993a2deb9 Mon Sep 17 00:00:00 2001 From: Keewis Date: Tue, 26 Jul 2022 16:51:48 +0200 Subject: [PATCH 7/7] update the doctest for indexes --- xarray/core/dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 80725b1bc11..92e83b2204c 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2961,7 +2961,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`.