diff options
author | Allan Haldane <allan.haldane@gmail.com> | 2017-06-29 15:20:00 -0400 |
---|---|---|
committer | Allan Haldane <allan.haldane@gmail.com> | 2017-11-08 19:15:44 -0500 |
commit | 2461bc91ba0eb97e66357d6454d1df5836a705cf (patch) | |
tree | e742ec6cd9e65940b509ea0e1e8e5fa2e6d159c3 /numpy/core/arrayprint.py | |
parent | 1368cbb696ae27b849eed67b4fd31c550a55dad5 (diff) | |
download | numpy-2461bc91ba0eb97e66357d6454d1df5836a705cf.tar.gz |
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
Diffstat (limited to 'numpy/core/arrayprint.py')
-rw-r--r-- | numpy/core/arrayprint.py | 27 |
1 files changed, 12 insertions, 15 deletions
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): """ |