8000 MAINT: fixups to legacy spacing in FloatFormat · numpy/numpy@710e032 · GitHub
[go: up one dir, main page]

Skip to content

Commit 710e032

Browse files
committed
MAINT: fixups to legacy spacing in FloatFormat
1 parent 6bfef27 commit 710e032

File tree

3 files changed

+24
-39
lines changed

3 files changed

+24
-39
lines changed

doc/release/1.14.0-notes.rst

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,15 @@ This option controls printing of the sign of floating-point types, and may be
310310
one of the characters '-', '+' or ' ', or the string 'legacy'. With '+' numpy
311311
always prints the sign of positive values, with ' ' it always prints a space
312312
(whitespace character) in the sign position of positive values, and with '-' it
313-
will omit the sign character for positive values. The new default is '-'.
313+
will omit the sign character for positive values, and with 'legacy' it will
314+
behave like ' ' except no space is printed in 0d arrays. The new default is '-'.
314315

315-
Setting ``sign='legacy'`` will behave like ' ' except that no space is printed
316-
in 0d arrays, and also includes a space before ``True`` values in size-1 bool
317-
arrays. This approximates the behavior of numpy 1.13 and before.
318-
319-
Unneeded whitespace in float and bool array printing removed
320-
------------------------------------------------------------
316+
Unneeded whitespace in float array printing removed
317+
---------------------------------------------------
321318
The new default of ``sign='-'`` (see last note) means that the ``repr`` of
322319
float arrays now often omits the whitespace characters previously used to
323320
display the sign. This new behavior can be disabled to mostly reproduce numpy
324-
1.13 behavior by calling:
325-
326-
>>> np.set_printoptions(sign='legacy')
327-
328-
Additionally, the ``repr`` of bool arrays with only one element now omits the
329-
whitespace before a ``True`` value, so that ``repr(array([True]))`` now returns
330-
``'array([True])'`` instead of ``'array([ True])'``. This is disabled by
331-
setting ``sign='legacy'``.
321+
1.13 behavior by calling ``np.set_printoptions(sign='legacy')``.
332322

333323
``threshold`` and ``edgeitems`` options added to ``np.array2string``
334324
-----------------------------------------------------------------

numpy/core/arrayprint.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
119119
print the sign of positive values. If ' ', always prints a space
120120
(whitespace character) in the sign position of positive values. If
121121
'-', omit the sign character of positive values. If 'legacy', print a
122-
space for positive values except in 0d arrays, and also add a space for
123-
'True' values in size-1 bool arrays. (default '-')
122+
space for positive values except in 0d arrays. (default '-')
124123
formatter : dict of callables, optional
125124
If not None, the keys should indicate the type(s) that the respective
126125
formatting function applies to. Callables should return a string.
@@ -263,7 +262,7 @@ def _get_formatdict(data, **opt):
263262
prec, supp, sign = opt['precision'], opt['suppress'], opt['sign']
264263

265264
# wrapped in lambdas to avoid taking a code path with the wrong type of data
266-
formatdict = {'bool': lambda: BoolFormat(data, legacy=(sign == 'legacy')),
265+
formatdict = {'bool': lambda: BoolFormat(data),
267266
'int': lambda: IntegerFormat(data),
268267
'float': lambda: FloatFormat(data, prec, supp, sign),
269268
'longfloat': lambda: LongFloatFormat(prec),
@@ -379,7 +378,6 @@ def wrapper(self, *args, **kwargs):
379378
# gracefully handle recursive calls, when object arrays contain themselves
380379
@_recursive_guard()
381380
def _array2string(a, options, separator=' ', prefix=""):
382-
383381
if a.size > options['threshold']:
384382
summary_insert = "..., "
385383
data = _leading_trailing(a)
@@ -401,7 +399,6 @@ def _array2string(a, options, separator=' ', prefix=""):
401399
return lst
402400

403401

404-
405402
def array2string(a, max_line_width=None, precision=None,
406403
suppress_small=None, separator=' ', prefix="",
407404
style=np._NoValue, formatter=None, threshold=None,
@@ -471,8 +468,7 @@ def array2string(a, max_line_width=None, precision=None,
471468
print the sign of positive values. If ' ', always prints a space
472469
(whitespace character) in the sign position of positive values. If
473470
'-', omit the sign character of positive values. If 'legacy', print a
474-
space for positive values except in 0d arrays, and also add a space for
475-
'True' values in size-1 bool arrays.
471+
space for positive values except in 0d arrays.
476472
477473
Returns
478474
-------
@@ -613,20 +609,18 @@ def __init__(self, data, precision, suppress_small, sign=False):
613609
if isinstance(sign, bool):
614610
sign = '+' if sign else '-'
615611

612+
self._legacy = False
616613
if sign == 'legacy':
614+
self._legacy = True
617615
sign = '-' if data.shape == () else ' '
618616

619617
self.precision = precision
620618
self.suppress_small = suppress_small
621619
self.sign = sign
622620
self.exp_format = False
623621
self.large_exponent = False
624-
try:
625-
self.fillFormat(data)
626-
except (NotImplementedError):
627-
# if reduce(data) fails, this instance will not be called, just
628-
# instantiated in formatdict.
629-
pass
622+
623+
self.fillFormat(data)
630624

631625
def fillFormat(self, data):
632626
with errstate(all='ignore'):
@@ -653,8 +647,9 @@ def fillFormat(self, data):
653647
self.large_exponent = 0 < min_val < 1e-99 or max_val >= 1e100
654648

655649
signpos = self.sign != '-' or any(non_zero < 0)
656-
# for back-compatibility with np 1.13, add extra space if padded
657-
signpos = signpos if self.sign != ' ' else 2
650+
# for back-compatibility with np 1.13, use two spaces
651+
if self._legacy:
652+
signpos = 2
658653
max_str_len = signpos + 6 + self.precision + self.large_exponent
659654

660655
conversion = '' if self.sign == '-' else self.sign
@@ -739,15 +734,9 @@ def __call__(self, x):
739734

740735
class BoolFormat(object):
741736
def __init__(self, data, **kwargs):
742-
# in legacy printing style, include a space before True except in 0d
743-
if kwargs.get('legacy', False):
744-
self.truestr = ' True' if data.shape != () else 'True'
745-
return
746-
747737
# add an extra space so " True" and "False" have the same length and
748-
# array elements align nicely when printed, but only for arrays with
749-
# more than one element (0d and nd)
750-
self.truestr = ' True' if data.size > 1 else 'True'
738+
# array elements align nicely when printed, except in 0d arrays
739+
self.truestr = ' True' if data.shape != () else 'True'
751740

752741
def __call__(self, x):
753742
return self.truestr if x else "False"

numpy/core/tests/test_arrayprint.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,28 +274,34 @@ def test_bool_spacing(self):
274274
assert_equal(repr(np.array([True, False])),
275275
'array([ True, False], dtype=bool)')
276276
assert_equal(repr(np.array([True])),
277-
'array([True], dtype=bool)')
277+
'array([ True], dtype=bool)')
278278
assert_equal(repr(np.array(True)),
279279
'array(True, dtype=bool)')
280280
assert_equal(repr(np.array(False)),
281281
'array(False, dtype=bool)')
282282

283283
def test_sign_spacing(self):
284284
a = np.arange(4.)
285+
b = np.array([1.234e9])
286+
285287
assert_equal(repr(a), 'array([0., 1., 2., 3.])')
286288
assert_equal(repr(np.array(1.)), 'array(1.)')
289+
assert_equal(repr(b), 'array([1.23400000e+09])')
287290

288291
np.set_printoptions(sign=' ')
289292
assert_equal(repr(a), 'array([ 0., 1., 2., 3.])')
290293
assert_equal(repr(np.array(1.)), 'array( 1.)')
294+
assert_equal(repr(b), 'array([ 1.23400000e+09])')
291295

292296
np.set_printoptions(sign='+')
293297
assert_equal(repr(a), 'array([+0., +1., +2., +3.])')
294298
assert_equal(repr(np.array(1.)), 'array(+1.)')
299+
assert_equal(repr(b), 'array([+1.23400000e+09])')
295300

296301
np.set_printoptions(sign='legacy')
297302
assert_equal(repr(a), 'array([ 0., 1., 2., 3.])')
298303
assert_equal(repr(np.array(1.)), 'array(1.)')
304+
assert_equal(repr(b), 'array([ 1.23400000e+09])')
299305

300306
def test_sign_spacing_structured(self):
301307
a = np.ones(2, dtype='f,f')

0 commit comments

Comments
 (0)
0