From bd80585cdae1d43fabb30ae0e184c2e40deb11e6 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Mon, 13 Nov 2017 23:36:44 -0800 Subject: MAINT: Add helper function to determine whether to show dtype in repr --- numpy/core/arrayprint.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index da173625e..8435574bf 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -1135,6 +1135,36 @@ if issubclass(intc, int): if issubclass(longlong, int): _typelessdata.append(longlong) + +def dtype_is_implied(dtype): + """ + Determine if the given dtype is implied by the representation of its values. + + Parameters + ---------- + dtype : dtype + Data type + + Returns + ------- + implied : bool + True if the dtype is implied by the representation of its values. + + Examples + -------- + >>> np.core.arrayprint.dtype_is_implied(int) + True + >>> np.array([1, 2, 3], int) + array([1, 2, 3]) + >>> np.core.arrayprint.dtype_is_implied(np.int8) + False + >>> np.array([1, 2, 3], np.int8) + array([1, 2, 3], dtype=np.int8) + """ + dtype = np.dtype(dtype) + return dtype.type in _typelessdata + + def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): """ Return the string representation of an array. @@ -1190,7 +1220,7 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): else: # show zero-length shape unless it is (0,) lst = "[], shape=%s" % (repr(arr.shape),) - skipdtype = (arr.dtype.type in _typelessdata) and arr.size > 0 + skipdtype = dtype_is_implied(arr.dtype) and arr.size > 0 if skipdtype: return "%s(%s)" % (class_name, lst) -- cgit v1.2.1 From ac6b1a902b99e340cf7eeeeb7392c91e38db9dd8 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Mon, 13 Nov 2017 23:45:45 -0800 Subject: ENH: don't show boolean dtype, as it is implied --- numpy/core/arrayprint.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 8435574bf..55682f393 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -1129,7 +1129,7 @@ def _void_scalar_repr(x): return StructureFormat.from_data(array(x), **_format_options)(x) -_typelessdata = [int_, float_, complex_] +_typelessdata = [int_, float_, complex_, bool_] if issubclass(intc, int): _typelessdata.append(intc) if issubclass(longlong, int): @@ -1162,6 +1162,8 @@ def dtype_is_implied(dtype): array([1, 2, 3], dtype=np.int8) """ dtype = np.dtype(dtype) + if _format_options['legacy'] and dtype.type == bool_: + return False return dtype.type in _typelessdata -- cgit v1.2.1