From 37eb8780941b879b9cc4c525ab5c9feba380ac79 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 27 Mar 2020 16:43:09 -0400 Subject: [PATCH 1/5] limit length of dataarray reprs --- xarray/core/formatting.py | 15 +++++++++++++-- xarray/tests/test_formatting.py | 13 +++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 89246ff228d..34892c9e117 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -3,7 +3,7 @@ import contextlib import functools from datetime import datetime, timedelta -from itertools import zip_longest +from itertools import chain, zip_longest import numpy as np import pandas as pd @@ -408,6 +408,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) @@ -433,7 +444,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: diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 61ecf46b79b..191949756ff 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -394,10 +394,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, 30 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) == 43 From ce1e5a356147898a5c6e81680fac903bfb429b94 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 27 Mar 2020 17:14:44 -0400 Subject: [PATCH 2/5] repr depends on numpy versions --- xarray/tests/test_formatting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 191949756ff..0a453169267 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -409,4 +409,4 @@ def test_large_array_repr_length(): da = xr.DataArray(np.random.randn(100, 5, 1)) result = repr(da).splitlines() - assert len(result) == 43 + assert len(result) < 50 From cf696a5f91d38a43a16675118754fe7bba222a5a Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Thu, 2 Apr 2020 17:25:09 -0400 Subject: [PATCH 3/5] whatsnew --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a138dee4128..9274add418d 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -38,6 +38,9 @@ New Features - Limited the length of array items with long string reprs to a reasonable width (:pull:`3900`) By `Maximilian Roos `_ +- Limited the number of lines of large arrays when numpy reprs would have greater than 40. + (:pull:`3905`) + By `Maximilian Roos `_ Bug fixes From fe9f7a2a315e147c1839daf7a8fe2172034dc456 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Mon, 25 May 2020 15:30:17 -0700 Subject: [PATCH 4/5] correct comment based on @keewis comment --- xarray/tests/test_formatting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 11a208901bd..82de8080c80 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -409,7 +409,7 @@ def test_short_numpy_repr(): ] # number of lines: # for default numpy repr: 167, 140, 254, 248, 599 - # for short_numpy_repr: 1, 7, 24, 19, 30 + # 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 From 2f29a1805e4f2b6f6951f16d9d92295adf755d9a Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Wed, 24 Jun 2020 14:43:01 +0000 Subject: [PATCH 5/5] Update whats-new.rst --- doc/whats-new.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 974b9025abb..ea3e32d3a80 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -54,9 +54,6 @@ New Features By `Andrew Williams `_ and `Robin Beer `_. - Added :py:meth:`DataArray.polyfit` and :py:func:`xarray.polyval` for fitting polynomials. (:issue:`3349`, :pull:`3733`, :pull:`4099`) By `Pascal Bourgault `_. -- Implement :py:meth:`DataArray.idxmax`, :py:meth:`DataArray.idxmin`, - :py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`. (:issue:`60`, :pull:`3871`) - By `Todd Jennings `_ - Control over attributes of result in :py:func:`merge`, :py:func:`concat`, :py:func:`combine_by_coords` and :py:func:`combine_nested` using combine_attrs keyword argument. (:issue:`3865`, :pull:`3877`)