diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 94c7e6020dc..b72b1232784 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -23,8 +23,7 @@ v2024.02.0 (unreleased) New Features ~~~~~~~~~~~~ -- Added a simple `nbytes` representation in DataArrays and Dataset `repr` (opt-in). - To enable, use `xr.set_options(display_nbytes=True)` +- Added a simple `nbytes` representation in DataArrays and Dataset `repr`. (:issue:`8690`, :pull:`8702`). By `Etienne Schalk `_. - Allow negative frequency strings (e.g. ``"-1YE"``). These strings are for example used diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 291e2ad8f59..5c4aed6e39e 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -337,10 +337,9 @@ def summarize_variable( else: dims_str = "" - if OPTIONS["display_nbytes"]: - nbytes_str = f" {render_human_readable_nbytes(variable.nbytes, attempt_constant_width=True)}" - else: - nbytes_str = "" + nbytes_str = ( + f" {render_human_readable_nbytes(variable.nbytes, attempt_constant_width=True)}" + ) front_str = f"{first_col}{dims_str}{variable.dtype}{nbytes_str} " values_width = max_width - len(front_str) @@ -676,12 +675,9 @@ def array_repr(arr): start = f"", + f"{start}({dims})> Size: {nbytes_str}", data_repr, ] if hasattr(arr, "coords"): @@ -716,11 +712,8 @@ def array_repr(arr): @recursive_repr("") def dataset_repr(ds): - if OPTIONS["display_nbytes"]: - nbytes_str = f" {render_human_readable_nbytes(ds.nbytes)}" - else: - nbytes_str = "" - summary = [f""] + nbytes_str = render_human_readable_nbytes(ds.nbytes) + summary = [f" Size: {nbytes_str}"] col_width = _calculate_col_width(ds.variables) max_rows = OPTIONS["display_max_rows"] diff --git a/xarray/core/options.py b/xarray/core/options.py index 944170e0a17..d116c350991 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -46,7 +46,6 @@ class T_Options(TypedDict): display_expand_data: Literal["default", True, False] display_expand_indexes: Literal["default", True, False] display_default_indexes: Literal["default", True, False] - display_nbytes: Literal["default", True, False] enable_cftimeindex: bool file_cache_maxsize: int keep_attrs: Literal["default", True, False] @@ -71,7 +70,6 @@ class T_Options(TypedDict): "display_expand_data": "default", "display_expand_indexes": "default", "display_default_indexes": False, - "display_nbytes": False, "enable_cftimeindex": True, "file_cache_maxsize": 128, "keep_attrs": "default", diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 708d249a9b8..f7770f4477a 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -316,12 +316,12 @@ def test_diff_array_repr(self) -> None: R array([1, 2], dtype=int64) Differing coordinates: - L * x (x) %cU1 'a' 'b' - R * x (x) %cU1 'a' 'c' + L * x (x) %cU1 8B 'a' 'b' + R * x (x) %cU1 8B 'a' 'c' Coordinates only on the left object: - * y (y) int64 1 2 3 + * y (y) int64 24B 1 2 3 Coordinates only on the right object: - label (x) int64 1 2 + label (x) int64 16B 1 2 Differing attributes: L units: m R units: kg @@ -436,22 +436,22 @@ def test_diff_dataset_repr(self) -> None: Differing dimensions: (x: 2, y: 3) != (x: 2) Differing coordinates: - L * x (x) %cU1 'a' 'b' + L * x (x) %cU1 8B 'a' 'b' Differing variable attributes: foo: bar - R * x (x) %cU1 'a' 'c' + R * x (x) %cU1 8B 'a' 'c' Differing variable attributes: source: 0 foo: baz Coordinates only on the left object: - * y (y) int64 1 2 3 + * y (y) int64 24B 1 2 3 Coordinates only on the right object: - label (x) int64 1 2 + label (x) int64 16B 1 2 Differing data variables: - L var1 (x, y) int64 1 2 3 4 5 6 - R var1 (x) int64 1 2 + L var1 (x, y) int64 48B 1 2 3 4 5 6 + R var1 (x) int64 16B 1 2 Data variables only on the left object: - var2 (x) int64 3 4 + var2 (x) int64 16B 3 4 Differing attributes: L title: mytitle R title: newtitle @@ -472,7 +472,7 @@ def test_array_repr(self) -> None: actual = formatting.array_repr(ds_12) expected = dedent( """\ - + Size: 8B array([0]) Dimensions without coordinates: test""" ) @@ -491,7 +491,7 @@ def test_array_repr(self) -> None: actual = formatting.array_repr(ds[(1, 2)]) expected = dedent( """\ - + Size: 8B 0 Dimensions without coordinates: test""" ) @@ -832,46 +832,23 @@ def test_display_nbytes() -> None: # Note: int16 is used to ensure that dtype is shown in the # numpy array representation for all OSes included Windows - with xr.set_options(display_nbytes=False): - actual = repr(xds) - expected = """ - -Dimensions: (foo: 1200, bar: 111) -Coordinates: - * foo (foo) int16 0 1 2 3 4 5 6 7 ... 1193 1194 1195 1196 1197 1198 1199 - * bar (bar) int16 0 1 2 3 4 5 6 7 8 ... 103 104 105 106 107 108 109 110 -Data variables: - *empty* - """.strip() - assert actual == expected - - actual = repr(xds["foo"]) - expected = """ - -array([ 0, 1, 2, ..., 1197, 1198, 1199], dtype=int16) -Coordinates: - * foo (foo) int16 0 1 2 3 4 5 6 7 ... 1193 1194 1195 1196 1197 1198 1199 -""".strip() - assert actual == expected - - with xr.set_options(display_nbytes=True): - actual = repr(xds) - expected = """ - + actual = repr(xds) + expected = """ + Size: 3kB Dimensions: (foo: 1200, bar: 111) Coordinates: * foo (foo) int16 2kB 0 1 2 3 4 5 6 ... 1194 1195 1196 1197 1198 1199 * bar (bar) int16 222B 0 1 2 3 4 5 6 7 ... 104 105 106 107 108 109 110 Data variables: *empty* - """.strip() - assert actual == expected + """.strip() + assert actual == expected - actual = repr(xds["foo"]) - expected = """ - + actual = repr(xds["foo"]) + expected = """ + Size: 2kB array([ 0, 1, 2, ..., 1197, 1198, 1199], dtype=int16) Coordinates: * foo (foo) int16 2kB 0 1 2 3 4 5 6 ... 1194 1195 1196 1197 1198 1199 """.strip() - assert actual == expected + assert actual == expected