Skip to content

Commit 6920363

Browse files
JustinZhengBCTomAugspurger
authored andcommitted
BUG-22984 Fix truncation of DataFrame representations (#22987)
* BUG-22984 Fix truncation of DataFrame representations
1 parent b4b945a commit 6920363

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

doc/source/whatsnew/v0.24.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,8 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
13191319
- :func:`read_sas()` will correctly parse sas7bdat files with many columns (:issue:`22628`)
13201320
- :func:`read_sas()` will correctly parse sas7bdat files with data page types having also bit 7 set (so page type is 128 + 256 = 384) (:issue:`16615`)
13211321
- Bug in :meth:`detect_client_encoding` where potential ``IOError`` goes unhandled when importing in a mod_wsgi process due to restricted access to stdout. (:issue:`21552`)
1322-
- Bug in :func:`to_string()` that broke column alignment when ``index=False`` and width of first column's values is greater than the width of first column's header (:issue:`16839`, :issue:`13032`)
1322+
- Bug in :func:`DataFrame.to_string()` that broke column alignment when ``index=False`` and width of first column's values is greater than the width of first column's header (:issue:`16839`, :issue:`13032`)
1323+
- Bug in :func:`DataFrame.to_string()` that caused representations of :class:`DataFrame` to not take up the whole window (:issue:`22984`)
13231324
- Bug in :func:`DataFrame.to_csv` where a single level MultiIndex incorrectly wrote a tuple. Now just the value of the index is written (:issue:`19589`).
13241325
- Bug in :meth:`HDFStore.append` when appending a :class:`DataFrame` with an empty string column and ``min_itemsize`` < 8 (:issue:`12242`)
13251326
- Bug in :meth:`read_csv()` in which :class:`MultiIndex` index names were being improperly handled in the cases when they were not provided (:issue:`23484`)

pandas/io/formats/format.py

-5
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,6 @@ def to_string(self):
608608
else: # max_cols == 0. Try to fit frame to terminal
609609
text = self.adj.adjoin(1, *strcols).split('\n')
610610
max_len = Series(text).str.len().max()
611-
headers = [ele[0] for ele in strcols]
612-
# Size of last col determines dot col size. See
613-
# `self._to_str_columns
614-
size_tr_col = len(headers[self.tr_size_col])
615-
max_len += size_tr_col # Need to make space for largest row
616611
# plus truncate dot col
617612
dif = max_len - self.w
618613
# '+ 1' to avoid too wide repr (GH PR #17023)

pandas/tests/io/formats/test_format.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,10 @@ def test_repr_non_interactive(self):
305305
assert not has_truncated_repr(df)
306306
assert not has_expanded_repr(df)
307307

308-
def test_repr_truncates_terminal_size(self):
308+
def test_repr_truncates_terminal_size(self, mock):
309309
# https://github.com/pandas-dev/pandas/issues/21180
310310
# TODO: use mock fixutre.
311311
# This is being backported, so doing it directly here.
312-
try:
313-
from unittest import mock
314-
except ImportError:
315-
mock = pytest.importorskip("mock")
316312

317313
terminal_size = (118, 96)
318314
p1 = mock.patch('pandas.io.formats.console.get_terminal_size',
@@ -343,6 +339,17 @@ def test_repr_truncates_terminal_size(self):
343339

344340
assert df2.columns[0] in result.split('\n')[0]
345341

342+
def test_repr_truncates_terminal_size_full(self, mock):
343+
# GH 22984 ensure entire window is filled
344+
terminal_size = (80, 24)
345+
df = pd.DataFrame(np.random.rand(1, 7))
346+
p1 = mock.patch('pandas.io.formats.console.get_terminal_size',
347+
return_value=terminal_size)
348+
p2 = mock.patch('pandas.io.formats.format.get_terminal_size',
349+
return_value=terminal_size)
350+
with p1, p2:
351+
assert "..." not in str(df)
352+
346353
def test_repr_max_columns_max_rows(self):
347354
term_width, term_height = get_terminal_size()
348355
if term_width < 10 or term_height < 10:

0 commit comments

Comments
 (0)