-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
BUG: Fix MI repr with long names #21655
BUG: Fix MI repr with long names #21655
Conversation
from @jorisvandenbossche
Yeah, agreed that would be best, but I can't tell whose doing the wrapping, if that makes sense. |
Well, I guess it has to be the terminal... So we should just always print two columns? And let the terminal wrap if needed? |
Codecov Report
@@ Coverage Diff @@
## master #21655 +/- ##
=========================================
Coverage ? 91.9%
=========================================
Files ? 154
Lines ? 49657
Branches ? 0
=========================================
Hits ? 45638
Misses ? 4019
Partials ? 0
Continue to review full report at Codecov.
|
Running import pandas as pd
import numpy as np
s = pd.DataFrame({"A" * 41: 1, "B" * 41: 1}, index=[0, 1, 2])
print("Regular Columns:")
print(repr(s))
print('\n\n')
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)
print("MultiIndex")
print(repr(df)) output
|
The output in the top-post is not longer correct with the latest changes? |
Correct, #21655 (comment) has the current output. |
# TODO: use mock fixutre. | ||
# This is being backported, so doing it directly here. | ||
try: | ||
from unittest import mock |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pandas/io/formats/format.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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
....
I can confirm that this fixes #21327 (the one that I could reproduce in my local console without any hacks) |
(cherry picked from commit ad76ffc)
(cherry picked from commit ad76ffc)
Closes #21180
output:
This matches the repr for non-hierarchical
output:
These can certainly be improved, though I'm not sure we'll (I'll) get to it for 0.23.2.