Skip to content

Commit

Permalink
BUG-22984 Fix truncation of DataFrame representations (pandas-dev#22987)
Browse files Browse the repository at this point in the history
* BUG-22984 Fix truncation of DataFrame representations
  • Loading branch information
JustinZhengBC authored and tm9k1 committed Nov 19, 2018
1 parent 7520954 commit 32a50dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,8 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
- :func:`read_sas()` will correctly parse sas7bdat files with many columns (:issue:`22628`)
- :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`)
- 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`)
- 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`)
- 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`)
- Bug in :func:`DataFrame.to_string()` that caused representations of :class:`DataFrame` to not take up the whole window (:issue:`22984`)
- 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`).
- Bug in :meth:`HDFStore.append` when appending a :class:`DataFrame` with an empty string column and ``min_itemsize`` < 8 (:issue:`12242`)
- 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`)
Expand Down
5 changes: 0 additions & 5 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,6 @@ def to_string(self):
else: # max_cols == 0. Try to fit frame to terminal
text = self.adj.adjoin(1, *strcols).split('\n')
max_len = Series(text).str.len().max()
headers = [ele[0] for ele in strcols]
# Size of last col determines dot col size. See
# `self._to_str_columns
size_tr_col = len(headers[self.tr_size_col])
max_len += size_tr_col # Need to make space for largest row
# plus truncate dot col
dif = max_len - self.w
# '+ 1' to avoid too wide repr (GH PR #17023)
Expand Down
17 changes: 12 additions & 5 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,10 @@ def test_repr_non_interactive(self):
assert not has_truncated_repr(df)
assert not has_expanded_repr(df)

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

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

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

def test_repr_truncates_terminal_size_full(self, mock):
# GH 22984 ensure entire window is filled
terminal_size = (80, 24)
df = pd.DataFrame(np.random.rand(1, 7))
p1 = mock.patch('pandas.io.formats.console.get_terminal_size',
return_value=terminal_size)
p2 = mock.patch('pandas.io.formats.format.get_terminal_size',
return_value=terminal_size)
with p1, p2:
assert "..." not in str(df)

def test_repr_max_columns_max_rows(self):
term_width, term_height = get_terminal_size()
if term_width < 10 or term_height < 10:
Expand Down

0 comments on commit 32a50dc

Please sign in to comment.