Skip to content
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

Fix exception when display_expand_data=False for file-backed array. #5235

Merged
merged 7 commits into from
May 6, 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
7 changes: 5 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pandas.errors import OutOfBoundsDatetime

from .duck_array_ops import array_equiv
from .indexing import MemoryCachedArray
from .options import OPTIONS, _get_boolean_with_default
from .pycompat import dask_array_type, sparse_array_type
from .utils import is_duck_array
Expand Down Expand Up @@ -507,10 +508,12 @@ def array_repr(arr):
else:
name_str = ""

if _get_boolean_with_default("display_expand_data", default=True):
if _get_boolean_with_default("display_expand_data", default=True) or isinstance(
arr.variable._data, MemoryCachedArray
):
data_repr = short_data_repr(arr)
else:
data_repr = inline_variable_array_repr(arr, OPTIONS["display_width"])
data_repr = inline_variable_array_repr(arr.variable, OPTIONS["display_width"])

summary = [
"<xarray.{} {}({})>".format(type(arr).__name__, name_str, dim_summary(arr)),
Expand Down
21 changes: 21 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import xarray as xr
from xarray.core import formatting

from . import requires_netCDF4


class TestFormatting:
def test_get_indexer_at_least_n_items(self):
Expand Down Expand Up @@ -472,6 +474,25 @@ def test_large_array_repr_length():
assert len(result) < 50


@requires_netCDF4
def test_repr_file_collapsed(tmp_path):
arr = xr.DataArray(np.arange(300), dims="test")
arr.to_netcdf(tmp_path / "test.nc", engine="netcdf4")

with xr.open_dataarray(tmp_path / "test.nc") as arr, xr.set_options(
display_expand_data=False
):
actual = formatting.array_repr(arr)
expected = dedent(
"""\
<xarray.DataArray (test: 300)>
array([ 0, 1, 2, ..., 297, 298, 299])
Dimensions without coordinates: test"""
)

assert actual == expected


@pytest.mark.parametrize(
"display_max_rows, n_vars, n_attr",
[(50, 40, 30), (35, 40, 30), (11, 40, 30), (1, 40, 30)],
Expand Down