Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ Breaking changes
- By default ``to_netcdf()`` add a ``_FillValue = NaN`` attributes to float types.
By `Frederic Laliberte <https://github.com/laliberte>`_.

- ``repr`` on ``DataArray`` objects uses an shortened display for NumPy array
data (:issue:`TBD`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

- xarray no longer supports python 3.3, versions of dask prior to v0.9.0,
or versions of bottleneck prior to v1.0.

Expand Down
31 changes: 30 additions & 1 deletion xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import contextlib
from datetime import datetime, timedelta
import functools

Expand Down Expand Up @@ -336,6 +337,34 @@ def dim_summary(obj):
return u', '.join(elements)


@contextlib.contextmanager
def set_numpy_options(*args, **kwargs):
original = np.get_printoptions()
np.set_printoptions(*args, **kwargs)
yield
np.set_printoptions(**original)


def short_array_repr(array):
array = np.asarray(array)
# default to lower precision so a full (abbreviated) line can fit on
# one line with the default display_width
options = {
'precision': 6,
'linewidth': OPTIONS['display_width'],
'threshold': 200,
}
if array.ndim < 3:
edgeitems = 3
elif array.ndim == 3:
edgeitems = 2
else:
edgeitems = 1
options['edgeitems'] = edgeitems
with set_numpy_options(**options):
return repr(array)


def array_repr(arr):
# used for DataArray, Variable and IndexVariable
if hasattr(arr, 'name') and arr.name is not None:
Expand All @@ -349,7 +378,7 @@ def array_repr(arr):
if isinstance(getattr(arr, 'variable', arr)._data, dask_array_type):
summary.append(repr(arr.data))
elif arr._in_memory or arr.size < 1e5:
summary.append(repr(arr.values))
summary.append(short_array_repr(arr.values))
else:
summary.append(u'[%s values with dtype=%s]' % (arr.size, arr.dtype))

Expand Down
23 changes: 23 additions & 0 deletions xarray/test/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,26 @@ def test_format_timestamp_out_of_bounds(self):
expected = '2300-12-01'
result = formatting.format_timestamp(date)
self.assertEqual(result, expected)


def test_set_numpy_options():
original_options = np.get_printoptions()
with formatting.set_numpy_options(threshold=10):
assert len(repr(np.arange(500))) < 200
# original options are restored
assert np.get_printoptions() == original_options


def test_short_array_repr():
cases = [
np.random.randn(500),
np.random.randn(20, 20),
np.random.randn(5, 10, 15),
np.random.randn(5, 10, 15, 3),
]
# number of lines:
# for default numpy repr: 167, 140, 254, 248
# for short_array_repr: 1, 7, 24, 19
for array in cases:
num_lines = formatting.short_array_repr(array).count('\n') + 1
assert num_lines < 30