8000 Merge pull request #10182 from eric-wieser/fix-extra-space · numpy/numpy@0cd292d · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0cd292d

Browse files
authored
Merge pull request #10182 from eric-wieser/fix-extra-space
BUG: Extra space is inserted on first line for long elements
2 parents 5f01e54 + c9052f0 commit 0cd292d

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

doc/release/1.14.0-notes.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,26 +265,26 @@ In summary, the major changes are:
265265
* Float arrays printed in scientific notation no longer use fixed-precision,
266266
and now instead show the shortest unique representation.
267267
* The ``str`` of floating-point scalars is no longer truncated in python2.
268-
268+
269269
* For other data types:
270270

271271
* Non-finite complex scalars print like ``nanj`` instead of ``nan*j``.
272272
* ``NaT`` values in datetime arrays are now properly aligned.
273273
* Arrays and scalars of ``np.void`` datatype are now printed using hex
274274
notation.
275-
275+
276276
* For line-wrapping:
277277

278278
* The "dtype" part of ndarray reprs will now be printed on the next line
279279
if there isn't space on the last line of array output.
280280
* The ``linewidth`` format option is now always respected.
281281
The `repr` or `str` of an array will never exceed this, unless a single
282282
element is too wide.
283-
* All but the last line of array strings will contain the same number of
284-
elements.
285283
* The last line of an array string will never have more elements than earlier
286284
lines.
287-
285+
* An extra space is no longer inserted on the first line if the elements are
286+
too wide.
287+
288288
* For summarization (the use of ``...`` to shorten long arrays):
289289

290290
* A trailing comma is no longer inserted for ``str``.
@@ -294,7 +294,7 @@ In summary, the major changes are:
294294
order to summarize any but the last axis, newlines are now appended to that
295295
line to match its leading newlines and a trailing space character is
296296
removed.
297-
297+
298298
* ``MaskedArray`` arrays now separate printed elements with commas, always
299299
print the dtype, and correctly wrap the elements of long arrays to multiple
300300
lines. If there is more than 1 dimension, the array attributes are now

numpy/core/arrayprint.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,14 @@ def array2string(a, max_line_width=None, precision=None,
622622
return _array2string(a, options, separator, prefix)
623623

624624

625-
def _extendLine(s, line, word, line_width, next_line_prefix):
626-
if len(line) + len(word) > line_width:
625+
def _extendLine(s, line, word, line_width, next_line_prefix, legacy):
626+
needs_wrap = len(line) + len(word) > line_width
627+
if legacy != '1.13':
628+
s# don't wrap lines if it won't help
629+
if len(line) <= len(next_line_prefix):
630+
needs_wrap = False
631+
632+
if needs_wrap:
627633
s += line.rstrip() + "\n"
628634
line = next_line_prefix
629635
line += word
@@ -682,26 +688,30 @@ def recurser(index, hanging_indent, curr_width):
682688
line = hanging_indent
683689
for i in range(leading_items):
684690
word = recurser(index + (i,), next_hanging_indent, next_width)
685-
s, line = _extendLine(s, line, word, elem_width, hanging_indent)
691+
s, line = _extendLine(
692+
s, line, word, elem_width, hanging_indent, legacy)
686693
line += separator
687694

688695
if show_summary:
689-
s, line = _extendLine(s, line, summary_insert, elem_width, hanging_indent)
696+
s, line = _extendLine(
697+
s, line, summary_insert, elem_width, hanging_indent, legacy)
690698
if legacy == '1.13':
691699
line += ", "
692700
else:
693701
line += separator
694702

695703
for i in range(trailing_items, 1, -1):
696704
word = recurser(index + (-i,), next_hanging_indent, next_width)
697-
s, line = _extendLine(s, line, word, elem_width, hanging_indent)
705+
s, line = _extendLine(
706+
s, line, word, elem_width, hanging_indent, legacy)
698707
line += separator
699708

700709
if legacy == '1.13':
701710
# width of the seperator is not considered on 1.13
702711
elem_width = curr_width
703712
word = recurser(index + (-1,), next_hanging_indent, next_width)
704-
s, line = _extendLine(s, line, word, elem_width, hanging_indent)
713+
s, line = _extendLine(
714+
s, line, word, elem_width, hanging_indent, legacy)
705715

706716
s += line
707717

numpy/core/tests/test_arrayprint.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,17 @@ def make_str(a, width, **kw):
289289
' 11\n'
290290
' 11]]]')
291291

292+
def test_wide_element(self):
293+
a = np.array(['xxxxx'])
294+
assert_equal(
295+
np.array2string(a, max_line_width=5),
296+
"['xxxxx']"
297+
)
298+
assert_equal(
299+
np.array2string(a, max_line_width=5, legacy='1.13'),
300+
"[ 'xxxxx']"
301+
)
302+
292303

293304
class TestPrintOptions(object):
294305
"""Test getting and setting global print options."""

0 commit comments

Comments
 (0)
0