Skip to content

Limit length of dataarray reprs #3905

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 9 commits into from
Jun 24, 2020
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
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