Skip to content

Commit abfddda

Browse files
committed
BUG: float arrays with no non-zero elements break legacy mode
Fixes #10026
1 parent dd31f13 commit abfddda

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

numpy/core/arrayprint.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,8 @@ def fillFormat(self, data):
710710
with errstate(all='ignore'):
711711
hasinf = isinf(data)
712712
special = isnan(data) | hasinf
713-
valid = not_equal(data, 0) & ~special
714-
non_zero = data[valid]
713+
non_special = data[~special]
714+
non_zero = non_special[non_special != 0]
715715
abs_non_zero = absolute(non_zero)
716716
if len(non_zero) == 0:
717717
max_val = 0.
@@ -727,7 +727,7 @@ def fillFormat(self, data):
727727
or max_val/min_val > 1000.):
728728
self.exp_format = True
729729

730-
if len(non_zero) == 0:
730+
if len(non_special) == 0:
731731
self.pad_left = 0
732732
self.pad_right = 0
733733
self.trim = '.'
@@ -740,7 +740,7 @@ def fillFormat(self, data):
740740
trim, unique = 'k', False
741741
strs = (dragon4_scientific(x, precision=self.precision,
742742
unique=unique, trim=trim, sign=self.sign == '+')
743-
for x in non_zero)
743+
for x in non_special)
744744
frac_strs, _, exp_strs = zip(*(s.partition('e') for s in strs))
745745
int_part, frac_part = zip(*(s.split('.') for s in frac_strs))
746746
self.exp_size = max(len(s) for s in exp_strs) - 1
@@ -751,7 +751,7 @@ def fillFormat(self, data):
751751
# for back-compatibility with np 1.13, use two spaces and full prec
752752
if self._legacy == '1.13':
753753
# undo addition of sign pos below
754-
will_add_sign = all(non_zero > 0) and self.sign == ' '
754+
will_add_sign = all(non_special > 0) and self.sign == ' '
755755
self.pad_left = 3 - will_add_sign
756756
else:
757757
# this should be only 1 or two. Can be calculated from sign.
@@ -769,7 +769,7 @@ def fillFormat(self, data):
769769
fractional=True,
770770
unique=unique, trim=trim,
771771
sign=self.sign == '+')
772-
for x in non_zero)
772+
for x in non_special)
773773
int_part, frac_part = zip(*(s.split('.') for s in strs))
774774
self.pad_left = max(len(s) for s in int_part)
775775
self.pad_right = max(len(s) for s in frac_part)
@@ -784,7 +784,7 @@ def fillFormat(self, data):
784784
self.trim = '.'
785785

786786
# account for sign = ' ' by adding one to pad_left
787-
if len(non_zero) > 0 and all(non_zero > 0) and self.sign == ' ':
787+
if all(non_special >= 0) and self.sign == ' ':
788788
self.pad_left += 1
789789

790790
if any(special):

numpy/core/tests/test_arrayprint.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ def test_sign_spacing(self):
333333
assert_equal(repr(a), 'array([0., 1., 2., 3.])')
334334
assert_equal(repr(np.array(1.)), 'array(1.)')
335335
assert_equal(repr(b), 'array([1.234e+09])')
336+
assert_equal(repr(np.array([0.])), 'array([0.])')
336337

337338
np.set_printoptions(sign=' ')
338339
assert_equal(repr(a), 'array([ 0., 1., 2., 3.])')
@@ -349,6 +350,7 @@ def test_sign_spacing(self):
349350
assert_equal(repr(b), 'array([ 1.23400000e+09])')
350351
assert_equal(repr(-b), 'array([ -1.23400000e+09])')
351352
assert_equal(repr(np.array(1.)), 'array(1.0)')
353+
assert_equal(repr(np.array([0.])), 'array([ 0.])')
352354

353355
assert_raises(TypeError, np.set_printoptions, wrongarg=True)
354356

@@ -419,6 +421,7 @@ def test_floatmode(self):
419421
assert_equal(repr(w[::5]),
420422
"array([1.0000e+00, 1.0000e+05, 1.0000e+10, 1.0000e+15, 1.0000e+20])")
421423
assert_equal(repr(wp), "array([1.2340e+001, 1.0000e+002, 1.0000e+123])")
424+
assert_equal(repr(np.zeros(3)), "array([0.0000, 0.0000, 0.0000])")
422425
# for larger precision, representation error becomes more apparent:
423426
np.set_printoptions(floatmode='fixed', precision=8)
424427
assert_equal(repr(z),

0 commit comments

Comments
 (0)