Skip to content

Applying max_colwidth to the DataFrame index (#7856) #8954

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

Closed
wants to merge 7 commits into from
Closed
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,5 @@ Bug Fixes


- Bug in ``Series.values_counts`` with excluding ``NaN`` for categorical type ``Series`` with ``dropna=True`` (:issue:`9443`)

- Bug in ``DataFrameFormatter._get_formatted_index`` with not applying ``max_colwidth`` to the ``DataFrame`` index (:issue:`7856`)
3 changes: 3 additions & 0 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,9 @@ def _get_formatted_index(self, frame):
formatter=fmt)
else:
fmt_index = [index.format(name=show_index_names, formatter=fmt)]
fmt_index = [tuple(_make_fixed_width(
list(x), justify='left', minimum=(self.col_space or 0)))
for x in fmt_index]

adjoined = adjoin(1, *fmt_index).split('\n')

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,21 @@ def mkframe(n):
com.pprint_thing(df._repr_fits_horizontal_())
self.assertTrue(has_expanded_repr(df))

def test_str_max_colwidth(self):
# GH 7856
df = pd.DataFrame([{'a': 'foo', 'b': 'bar',
'c': 'uncomfortably long line with lots of stuff',
'd': 1},
{'a': 'foo', 'b': 'bar', 'c': 'stuff', 'd': 1}])
df.set_index(['a', 'b', 'c'])
self.assertTrue(str(df) == ' a b c d\n'
'0 foo bar uncomfortably long line with lots of stuff 1\n'
'1 foo bar stuff 1')
with option_context('max_colwidth', 20):
self.assertTrue(str(df) == ' a b c d\n'
'0 foo bar uncomfortably lo... 1\n'
'1 foo bar stuff 1')

def test_auto_detect(self):
term_width, term_height = get_terminal_size()
fac = 1.05 # Arbitrary large factor to exceed term widht
Expand Down