Skip to content

fix the variable repr with display_expand_data=False #5406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Bug fixes
- Fix 1-level multi-index incorrectly converted to single index (:issue:`5384`,
:pull:`5385`).
By `Benoit Bovy <https://github.com/benbovy>`_.
- Fix the ``repr`` of :py:class:`Variable` objects with ``display_expand_data=True``
(:pull:`5406`)
By `Justus Magin <https://github.com/keewis>`_.


Documentation
Expand Down
8 changes: 6 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,18 @@ def short_data_repr(array):


def array_repr(arr):
from .variable import Variable

# used for DataArray, Variable and IndexVariable
if hasattr(arr, "name") and arr.name is not None:
name_str = f"{arr.name!r} "
else:
name_str = ""

if _get_boolean_with_default("display_expand_data", default=True) or isinstance(
arr.variable._data, MemoryCachedArray
if (
isinstance(arr, Variable)
or _get_boolean_with_default("display_expand_data", default=True)
or isinstance(arr.variable._data, MemoryCachedArray)
):
data_repr = short_data_repr(arr)
else:
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ def test_array_repr(self):

assert actual == expected

def test_array_repr_variable(self):
var = xr.Variable("x", [0, 1])

formatting.array_repr(var)

with xr.set_options(display_expand_data=False):
formatting.array_repr(var)


def test_inline_variable_array_repr_custom_repr():
class CustomArray:
Expand Down