Skip to content

Commit fc9026b

Browse files
Illviljanmax-sixty
andauthored
Avoid loading any data for reprs (#7203)
* Avoid loading any data for reprs * Update test_formatting.py * Update whats-new.rst * Update doc/whats-new.rst Co-authored-by: Maximilian Roos <[email protected]> * specify dtype to avoid os issues * Update xarray/tests/test_formatting.py * Update whats-new.rst Co-authored-by: Maximilian Roos <[email protected]>
1 parent 7ff2b04 commit fc9026b

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

doc/whats-new.rst

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ New Features
3535
Breaking changes
3636
~~~~~~~~~~~~~~~~
3737

38+
- ``repr(ds)`` may not show the same result because it doesn't load small,
39+
lazy data anymore. Use ``ds.head().load()`` when wanting to see just a sample
40+
of the data. (:issue:`6722`, :pull:`7203`).
41+
By `Jimmy Westling <https://github.com/illviljan>`_.
3842
- Many arguments of plotmethods have been made keyword-only.
3943
- ``xarray.plot.plot`` module renamed to ``xarray.plot.dataarray_plot`` to prevent
4044
shadowing of the ``plot`` method. (:issue:`6949`, :pull:`7052`).

xarray/core/formatting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ def short_data_repr(array):
579579
return short_numpy_repr(array)
580580
elif is_duck_array(internal_data):
581581
return limit_lines(repr(array.data), limit=40)
582-
elif array._in_memory or array.size < 1e5:
582+
elif array._in_memory:
583583
return short_numpy_repr(array)
584584
else:
585585
# internal xarray array type

xarray/tests/test_formatting.py

+30-4
Original file line numberDiff line numberDiff line change
@@ -575,17 +575,28 @@ def test_large_array_repr_length() -> None:
575575

576576
@requires_netCDF4
577577
def test_repr_file_collapsed(tmp_path) -> None:
578-
arr = xr.DataArray(np.arange(300), dims="test")
579-
arr.to_netcdf(tmp_path / "test.nc", engine="netcdf4")
578+
arr_to_store = xr.DataArray(np.arange(300, dtype=np.int64), dims="test")
579+
arr_to_store.to_netcdf(tmp_path / "test.nc", engine="netcdf4")
580580

581581
with xr.open_dataarray(tmp_path / "test.nc") as arr, xr.set_options(
582582
display_expand_data=False
583583
):
584-
actual = formatting.array_repr(arr)
584+
actual = repr(arr)
585585
expected = dedent(
586586
"""\
587587
<xarray.DataArray (test: 300)>
588-
array([ 0, 1, 2, ..., 297, 298, 299])
588+
[300 values with dtype=int64]
589+
Dimensions without coordinates: test"""
590+
)
591+
592+
assert actual == expected
593+
594+
arr_loaded = arr.compute()
595+
actual = arr_loaded.__repr__()
596+
expected = dedent(
597+
"""\
598+
<xarray.DataArray (test: 300)>
599+
0 1 2 3 4 5 6 7 8 9 10 11 12 ... 288 289 290 291 292 293 294 295 296 297 298 299
589600
Dimensions without coordinates: test"""
590601
)
591602

@@ -699,3 +710,18 @@ def test__element_formatter(n_elements: int = 100) -> None:
699710
)
700711
actual = intro + values
701712
assert expected == actual
713+
714+
715+
def test_lazy_array_wont_compute() -> None:
716+
from xarray.core.indexing import LazilyIndexedArray
717+
718+
class LazilyIndexedArrayNotComputable(LazilyIndexedArray):
719+
def __array__(self, dtype=None):
720+
raise NotImplementedError("Computing this array is not possible.")
721+
722+
arr = LazilyIndexedArrayNotComputable(np.array([1, 2]))
723+
var = xr.DataArray(arr)
724+
725+
# These will crash if var.data are converted to numpy arrays:
726+
var.__repr__()
727+
var._repr_html_()

0 commit comments

Comments
 (0)