Skip to content

Commit d63c26c

Browse files
tomwhitekeewis
andauthored
Fix exception when display_expand_data=False for file-backed array. (#5235)
Co-authored-by: keewis <keewis@users.noreply.github.com>
1 parent 477fc2f commit d63c26c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

xarray/core/formatting.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pandas.errors import OutOfBoundsDatetime
1212

1313
from .duck_array_ops import array_equiv
14+
from .indexing import MemoryCachedArray
1415
from .options import OPTIONS, _get_boolean_with_default
1516
from .pycompat import dask_array_type, sparse_array_type
1617
from .utils import is_duck_array
@@ -507,10 +508,12 @@ def array_repr(arr):
507508
else:
508509
name_str = ""
509510

510-
if _get_boolean_with_default("display_expand_data", default=True):
511+
if _get_boolean_with_default("display_expand_data", default=True) or isinstance(
512+
arr.variable._data, MemoryCachedArray
513+
):
511514
data_repr = short_data_repr(arr)
512515
else:
513-
data_repr = inline_variable_array_repr(arr, OPTIONS["display_width"])
516+
data_repr = inline_variable_array_repr(arr.variable, OPTIONS["display_width"])
514517

515518
summary = [
516519
"<xarray.{} {}({})>".format(type(arr).__name__, name_str, dim_summary(arr)),

xarray/tests/test_formatting.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import xarray as xr
99
from xarray.core import formatting
1010

11+
from . import requires_netCDF4
12+
1113

1214
class TestFormatting:
1315
def test_get_indexer_at_least_n_items(self):
@@ -472,6 +474,25 @@ def test_large_array_repr_length():
472474
assert len(result) < 50
473475

474476

477+
@requires_netCDF4
478+
def test_repr_file_collapsed(tmp_path):
479+
arr = xr.DataArray(np.arange(300), dims="test")
480+
arr.to_netcdf(tmp_path / "test.nc", engine="netcdf4")
481+
482+
with xr.open_dataarray(tmp_path / "test.nc") as arr, xr.set_options(
483+
display_expand_data=False
484+
):
485+
actual = formatting.array_repr(arr)
486+
expected = dedent(
487+
"""\
488+
<xarray.DataArray (test: 300)>
489+
array([ 0, 1, 2, ..., 297, 298, 299])
490+
Dimensions without coordinates: test"""
491+
)
492+
493+
assert actual == expected
494+
495+
475496
@pytest.mark.parametrize(
476497
"display_max_rows, n_vars, n_attr",
477498
[(50, 40, 30), (35, 40, 30), (11, 40, 30), (1, 40, 30)],

0 commit comments

Comments
 (0)