From 2461bc91ba0eb97e66357d6454d1df5836a705cf Mon Sep 17 00:00:00 2001 From: Allan Haldane Date: Thu, 29 Jun 2017 15:20:00 -0400 Subject: ENH: fix 0d array printing using `str` or `formatter`. The str of 0d arrays now returns `str(a[()])` like scalars, and the repr returns `'array(' + formatter(a[()]) + ')'` like ndarrays. The default implementation of str and repr for user-defined types is removed. Fixes #1415 --- numpy/core/arrayprint.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 2706d16f0..f996aa394 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -416,8 +416,8 @@ def _array2string(a, options, separator=' ', prefix=""): def array2string(a, max_line_width=None, precision=None, suppress_small=None, separator=' ', prefix="", - style=np._NoValue, formatter=None, threshold=None, - edgeitems=None, sign=None, floatmode=None): + style=None, formatter=None, threshold=None, + edgeitems=None, sign=None): """ Return a string representation of an array. @@ -441,12 +441,11 @@ def array2string(a, max_line_width=None, precision=None, 'prefix(' + array2string(a) + ')' - The length of the prefix string is used to align the - output correctly. - style : _NoValue, optional - Has no effect, do not use. - - .. deprecated:: 1.14.0 + The length of the prefix string is used to align the output correctly. + style : None or function, optional + Controls the printing of 0d arrays. If `None`, prints the 0d array's + element using the usual array formatting. Otherwise, `style` should be + a function that accepts a numpy scalar and returns a string. formatter : dict of callables, optional If not None, the keys should indicate the type(s) that the respective formatting function applies to. Callables should return a string. @@ -541,18 +540,15 @@ def array2string(a, max_line_width=None, precision=None, '[0x0L 0x1L 0x2L]' """ - # Deprecation 05-16-2017 v1.14 - if style is not np._NoValue: - warnings.warn("'style' argument is deprecated and no longer functional", - DeprecationWarning, stacklevel=3) - overrides = _make_options_dict(precision, threshold, edgeitems, max_line_width, suppress_small, None, None, sign, formatter, floatmode) options = _format_options.copy() options.update(overrides) - if a.size == 0: + if style is not None and a.shape == (): + return style(a[()]) + elif a.size == 0: # treat as a null array if any of shape elements == 0 lst = "[]" else: @@ -1164,7 +1160,8 @@ def array_str(a, max_line_width=None, precision=None, suppress_small=None): '[0 1 2]' """ - return array2string(a, max_line_width, precision, suppress_small, ' ', "") + return array2string(a, max_line_width, precision, suppress_small, + ' ', "", str) def set_string_function(f, repr=True): """ -- cgit v1.2.1