Skip to content

BUG: Fix MI repr with long names #21655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.23.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Fixed Regressions

- Fixed regression in :meth:`to_csv` when handling file-like object incorrectly (:issue:`21471`)
- Bug in both :meth:`DataFrame.first_valid_index` and :meth:`Series.first_valid_index` raised for a row index having duplicate values (:issue:`21441`)
-
- Fixed printing of DataFrames with hierarchical columns with long names (:issue:`21180`)

.. _whatsnew_0232.performance:

Expand Down
4 changes: 3 additions & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ def to_string(self):
col_lens = col_lens.drop(mid_ix)
n_cols = len(col_lens)
max_cols_adj = n_cols - self.index # subtract index column
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make make the comments consistent here

# subtract index column
max_cols_adj = ...

# esnure we print at least two
....

# GH-21180. Ensure that we print at least two.
max_cols_adj = max(max_cols_adj, 2)
self.max_cols_adj = max_cols_adj

# Call again _chk_truncate to cut frame appropriately
Expand Down Expand Up @@ -778,7 +780,7 @@ def space_format(x, y):

str_columns = list(zip(*[[space_format(x, y) for y in x]
for x in fmt_columns]))
if self.sparsify:
if self.sparsify and len(str_columns):
str_columns = _sparsify(str_columns)

str_columns = [list(x) for x in zip(*str_columns)]
Expand Down
38 changes: 38 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,44 @@ 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):
# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should PR in 0.24.0 (to move this to test_decorators)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to lay out my plan.

I'd like to merge this with the try / except, backport to 0.23.2, and then make a PR removing this and using the mock fixture from #20729

#20729 isn't being backported, so that seems easiset.

except ImportError:
mock = pytest.importorskip("mock")

terminal_size = (118, 96)
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)

index = range(5)
columns = pd.MultiIndex.from_tuples([
('This is a long title with > 37 chars.', 'cat'),
('This is a loooooonger title with > 43 chars.', 'dog'),
])
df = pd.DataFrame(1, index=index, columns=columns)

with p1, p2:
result = repr(df)

h1, h2 = result.split('\n')[:2]
assert 'long' in h1
assert 'loooooonger' in h1
assert 'cat' in h2
assert 'dog' in h2

# regular columns
df2 = pd.DataFrame({"A" * 41: [1, 2], 'B' * 41: [1, 2]})
with p1, p2:
result = repr(df2)

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

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