Skip to content

Commit 83eda1a

Browse files
authored
fix the variable repr with display_expand_data=False (#5406)
* make sure formatting.array_repr still works with variables with display_expand_data=False * always use the short data repr for variables * update whats-new.rst [skip-ci]
1 parent 2b38adc commit 83eda1a

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Bug fixes
4747
- Fix 1-level multi-index incorrectly converted to single index (:issue:`5384`,
4848
:pull:`5385`).
4949
By `Benoit Bovy <https://github.com/benbovy>`_.
50+
- Fix the ``repr`` of :py:class:`Variable` objects with ``display_expand_data=True``
51+
(:pull:`5406`)
52+
By `Justus Magin <https://github.com/keewis>`_.
5053

5154

5255
Documentation

xarray/core/formatting.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,18 @@ def short_data_repr(array):
502502

503503

504504
def array_repr(arr):
505+
from .variable import Variable
506+
505507
# used for DataArray, Variable and IndexVariable
506508
if hasattr(arr, "name") and arr.name is not None:
507509
name_str = f"{arr.name!r} "
508510
else:
509511
name_str = ""
510512

511-
if _get_boolean_with_default("display_expand_data", default=True) or isinstance(
512-
arr.variable._data, MemoryCachedArray
513+
if (
514+
isinstance(arr, Variable)
515+
or _get_boolean_with_default("display_expand_data", default=True)
516+
or isinstance(arr.variable._data, MemoryCachedArray)
513517
):
514518
data_repr = short_data_repr(arr)
515519
else:

xarray/tests/test_formatting.py

+8
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ def test_array_repr(self):
404404

405405
assert actual == expected
406406

407+
def test_array_repr_variable(self):
408+
var = xr.Variable("x", [0, 1])
409+
410+
formatting.array_repr(var)
411+
412+
with xr.set_options(display_expand_data=False):
413+
formatting.array_repr(var)
414+
407415

408416
def test_inline_variable_array_repr_custom_repr():
409417
class CustomArray:

0 commit comments

Comments
 (0)