Skip to content

Commit

Permalink
Limit length of dataarray reprs (#3905)
Browse files Browse the repository at this point in the history
* limit length of dataarray reprs

* repr depends on numpy versions

* whatsnew

* correct comment based on @keewis comment

* Update whats-new.rst

Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
  • Loading branch information
max-sixty and dcherian authored Jun 24, 2020
1 parent a2dac23 commit f281b3b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 3 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ New Features
- Limited the length of array items with long string reprs to a
reasonable width (:pull:`3900`)
By `Maximilian Roos <https://github.com/max-sixty>`_
- Limited the number of lines of large arrays when numpy reprs would have greater than 40.
(:pull:`3905`)
By `Maximilian Roos <https://github.com/max-sixty>`_
- Implement :py:meth:`DataArray.idxmax`, :py:meth:`DataArray.idxmin`,
:py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`. (:issue:`60`, :pull:`3871`)
By `Todd Jennings <https://github.com/toddrjen>`_
Expand Down Expand Up @@ -96,7 +99,6 @@ New Features
By `Deepak Cherian <https://github.com/dcherian>`_
- :py:meth:`map_blocks` can now handle dask-backed xarray objects in ``args``. (:pull:`3818`)
By `Deepak Cherian <https://github.com/dcherian>`_

- Add keyword ``decode_timedelta`` to :py:func:`xarray.open_dataset`,
(:py:func:`xarray.open_dataarray`, :py:func:`xarray.open_dataarray`,
:py:func:`xarray.decode_cf`) that allows to disable/enable the decoding of timedeltas
Expand Down
15 changes: 13 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import contextlib
import functools
from datetime import datetime, timedelta
from itertools import zip_longest
from itertools import chain, zip_longest
from typing import Hashable

import numpy as np
Expand Down Expand Up @@ -422,6 +422,17 @@ def set_numpy_options(*args, **kwargs):
np.set_printoptions(**original)


def limit_lines(string: str, *, limit: int):
"""
If the string is more lines than the limit,
this returns the middle lines replaced by an ellipsis
"""
lines = string.splitlines()
if len(lines) > limit:
string = "\n".join(chain(lines[: limit // 2], ["..."], lines[-limit // 2 :]))
return string


def short_numpy_repr(array):
array = np.asarray(array)

Expand All @@ -447,7 +458,7 @@ def short_data_repr(array):
elif hasattr(internal_data, "__array_function__") or isinstance(
internal_data, dask_array_type
):
return repr(array.data)
return limit_lines(repr(array.data), limit=40)
elif array._in_memory or array.size < 1e5:
return short_numpy_repr(array)
else:
Expand Down
13 changes: 11 additions & 2 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,19 @@ def test_short_numpy_repr():
np.random.randn(20, 20),
np.random.randn(5, 10, 15),
np.random.randn(5, 10, 15, 3),
np.random.randn(100, 5, 1),
]
# number of lines:
# for default numpy repr: 167, 140, 254, 248
# for short_numpy_repr: 1, 7, 24, 19
# for default numpy repr: 167, 140, 254, 248, 599
# for short_numpy_repr: 1, 7, 24, 19, 25
for array in cases:
num_lines = formatting.short_numpy_repr(array).count("\n") + 1
assert num_lines < 30


def test_large_array_repr_length():

da = xr.DataArray(np.random.randn(100, 5, 1))

result = repr(da).splitlines()
assert len(result) < 50

0 comments on commit f281b3b

Please sign in to comment.