Skip to content

Commit

Permalink
PR comments: Removed global option + size format in repr header
Browse files Browse the repository at this point in the history
  • Loading branch information
etienneschalk committed Feb 4, 2024
1 parent aafacd4 commit 12bd33d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 63 deletions.
3 changes: 1 addition & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/etienneschalk>`_.
- Allow negative frequency strings (e.g. ``"-1YE"``). These strings are for example used
Expand Down
21 changes: 7 additions & 14 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -676,12 +675,9 @@ def array_repr(arr):

start = f"<xarray.{type(arr).__name__} {name_str}"
dims = dim_summary_limited(arr, col_width=len(start) + 1, max_rows=max_rows)
if OPTIONS["display_nbytes"]:
nbytes_str = f" {render_human_readable_nbytes(arr.nbytes)}"
else:
nbytes_str = ""
nbytes_str = render_human_readable_nbytes(arr.nbytes)
summary = [
f"{start}({dims}){nbytes_str}>",
f"{start}({dims})> Size: {nbytes_str}",
data_repr,
]
if hasattr(arr, "coords"):
Expand Down Expand Up @@ -716,11 +712,8 @@ def array_repr(arr):

@recursive_repr("<recursive Dataset>")
def dataset_repr(ds):
if OPTIONS["display_nbytes"]:
nbytes_str = f" {render_human_readable_nbytes(ds.nbytes)}"
else:
nbytes_str = ""
summary = [f"<xarray.{type(ds).__name__}{nbytes_str}>"]
nbytes_str = render_human_readable_nbytes(ds.nbytes)
summary = [f"<xarray.{type(ds).__name__}> Size: {nbytes_str}"]

col_width = _calculate_col_width(ds.variables)
max_rows = OPTIONS["display_max_rows"]
Expand Down
2 changes: 0 additions & 2 deletions xarray/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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",
Expand Down
67 changes: 22 additions & 45 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -472,7 +472,7 @@ def test_array_repr(self) -> None:
actual = formatting.array_repr(ds_12)
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)>
<xarray.DataArray (1, 2) (test: 1)> Size: 8B
array([0])
Dimensions without coordinates: test"""
)
Expand All @@ -491,7 +491,7 @@ def test_array_repr(self) -> None:
actual = formatting.array_repr(ds[(1, 2)])
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)>
<xarray.DataArray (1, 2) (test: 1)> Size: 8B
0
Dimensions without coordinates: test"""
)
Expand Down Expand Up @@ -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 = """
<xarray.Dataset>
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 = """
<xarray.DataArray 'foo' (foo: 1200)>
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 = """
<xarray.Dataset 3kB>
actual = repr(xds)
expected = """
<xarray.Dataset> 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 = """
<xarray.DataArray 'foo' (foo: 1200) 2kB>
actual = repr(xds["foo"])
expected = """
<xarray.DataArray 'foo' (foo: 1200)> 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

0 comments on commit 12bd33d

Please sign in to comment.