Skip to content

Commit 17ad791

Browse files
BUG (output formatting): use fixed with for truncation column instead of inferring from last column (#24905)
1 parent 0c319f5 commit 17ad791

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,7 @@ I/O
17551755
- Bug in :meth:`DataFrame.to_stata` and :class:`pandas.io.stata.StataWriter117` that produced invalid files when using strLs with non-ASCII characters (:issue:`23573`)
17561756
- Bug in :class:`HDFStore` that caused it to raise ``ValueError`` when reading a Dataframe in Python 3 from fixed format written in Python 2 (:issue:`24510`)
17571757
- Bug in :func:`DataFrame.to_string()` and more generally in the floating ``repr`` formatter. Zeros were not trimmed if ``inf`` was present in a columns while it was the case with NA values. Zeros are now trimmed as in the presence of NA (:issue:`24861`).
1758+
- Bug in the ``repr`` when truncating the number of columns and having a wide last column (:issue:`24849`).
17581759

17591760
Plotting
17601761
^^^^^^^^

pandas/io/formats/format.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,6 @@ def _chk_truncate(self):
435435
"""
436436
from pandas.core.reshape.concat import concat
437437

438-
# Column of which first element is used to determine width of a dot col
439-
self.tr_size_col = -1
440-
441438
# Cut the data to the information actually printed
442439
max_cols = self.max_cols
443440
max_rows = self.max_rows
@@ -556,10 +553,7 @@ def _to_str_columns(self):
556553

557554
if truncate_h:
558555
col_num = self.tr_col_num
559-
# infer from column header
560-
col_width = self.adj.len(strcols[self.tr_size_col][0])
561-
strcols.insert(self.tr_col_num + 1, ['...'.center(col_width)] *
562-
(len(str_index)))
556+
strcols.insert(self.tr_col_num + 1, [' ...'] * (len(str_index)))
563557
if truncate_v:
564558
n_header_rows = len(str_index) - len(frame)
565559
row_num = self.tr_row_num
@@ -577,8 +571,8 @@ def _to_str_columns(self):
577571
if ix == 0:
578572
dot_mode = 'left'
579573
elif is_dot_col:
580-
cwidth = self.adj.len(strcols[self.tr_size_col][0])
581-
dot_mode = 'center'
574+
cwidth = 4
575+
dot_mode = 'right'
582576
else:
583577
dot_mode = 'right'
584578
dot_str = self.adj.justify([my_str], cwidth, mode=dot_mode)[0]

pandas/tests/io/formats/test_format.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,15 @@ def test_repr_truncates_terminal_size_full(self, monkeypatch):
345345
lambda: terminal_size)
346346
assert "..." not in str(df)
347347

348+
def test_repr_truncation_column_size(self):
349+
# dataframe with last column very wide -> check it is not used to
350+
# determine size of truncation (...) column
351+
df = pd.DataFrame({'a': [108480, 30830], 'b': [12345, 12345],
352+
'c': [12345, 12345], 'd': [12345, 12345],
353+
'e': ['a' * 50] * 2})
354+
assert "..." in str(df)
355+
assert " ... " not in str(df)
356+
348357
def test_repr_max_columns_max_rows(self):
349358
term_width, term_height = get_terminal_size()
350359
if term_width < 10 or term_height < 10:
@@ -543,7 +552,7 @@ def test_to_string_with_formatters_unicode(self):
543552
formatters={u('c/\u03c3'): lambda x: '{x}'.format(x=x)})
544553
assert result == u(' c/\u03c3\n') + '0 1\n1 2\n2 3'
545554

546-
def test_east_asian_unicode_frame(self):
555+
def test_east_asian_unicode_false(self):
547556
if PY3:
548557
_rep = repr
549558
else:
@@ -643,17 +652,23 @@ def test_east_asian_unicode_frame(self):
643652
u'ああああ': [u'さ', u'し', u'す', u'せ']},
644653
columns=['a', 'b', 'c', u'ああああ'])
645654

646-
expected = (u" a ... ああああ\n0 あああああ ... \n"
647-
u".. ... ... ...\n3 えええ ... \n"
655+
expected = (u" a ... ああああ\n0 あああああ ... さ\n"
656+
u".. ... ... ...\n3 えええ ... せ\n"
648657
u"\n[4 rows x 4 columns]")
649658
assert _rep(df) == expected
650659

651660
df.index = [u'あああ', u'いいいい', u'う', 'aaa']
652-
expected = (u" a ... ああああ\nあああ あああああ ... \n"
653-
u".. ... ... ...\naaa えええ ... \n"
661+
expected = (u" a ... ああああ\nあああ あああああ ... さ\n"
662+
u".. ... ... ...\naaa えええ ... せ\n"
654663
u"\n[4 rows x 4 columns]")
655664
assert _rep(df) == expected
656665

666+
def test_east_asian_unicode_true(self):
667+
if PY3:
668+
_rep = repr
669+
else:
670+
_rep = unicode # noqa
671+
657672
# Emable Unicode option -----------------------------------------
658673
with option_context('display.unicode.east_asian_width', True):
659674

@@ -757,18 +772,18 @@ def test_east_asian_unicode_frame(self):
757772
u'ああああ': [u'さ', u'し', u'す', u'せ']},
758773
columns=['a', 'b', 'c', u'ああああ'])
759774

760-
expected = (u" a ... ああああ\n"
761-
u"0 あああああ ... \n"
762-
u".. ... ... ...\n"
763-
u"3 えええ ... \n"
775+
expected = (u" a ... ああああ\n"
776+
u"0 あああああ ... さ\n"
777+
u".. ... ... ...\n"
778+
u"3 えええ ... せ\n"
764779
u"\n[4 rows x 4 columns]")
765780
assert _rep(df) == expected
766781

767782
df.index = [u'あああ', u'いいいい', u'う', 'aaa']
768-
expected = (u" a ... ああああ\n"
769-
u"あああ あああああ ... \n"
770-
u"... ... ... ...\n"
771-
u"aaa えええ ... \n"
783+
expected = (u" a ... ああああ\n"
784+
u"あああ あああああ ... さ\n"
785+
u"... ... ... ...\n"
786+
u"aaa えええ ... せ\n"
772787
u"\n[4 rows x 4 columns]")
773788
assert _rep(df) == expected
774789

0 commit comments

Comments
 (0)