Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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: 2 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,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
18 changes: 18 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4815,6 +4815,24 @@ def test_repr_unicode(self):
result = repr(df)
self.assertEqual(result.split('\n')[0].rstrip(), ex_top)

def test_str_max_colwidth(self):
# GH 7856
Copy link
Contributor

Choose a reason for hiding this comment

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

use with pd.option_context('max_colwidth',.....): so that it will only affect the code you want it to

curr_max_colwidth = pd.get_option('max_colwidth')
Copy link
Contributor

Choose a reason for hiding this comment

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

these tests should all be in test_format.py instead of here

# As we change a global option, we save it
Copy link
Contributor

Choose a reason for hiding this comment

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

pls add the issue number as a comment here

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'])
assert(str(df) == ' a b c d\n'
Copy link
Contributor

Choose a reason for hiding this comment

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

use self.assertTrue(.....) instead of bare assert

'0 foo bar uncomfortably long line with lots of stuff 1\n'
'1 foo bar stuff 1')
pd.set_option('max_colwidth', 20)
assert(str(df) == ' a b c d\n'
'0 foo bar uncomfortably lo... 1\n'
'1 foo bar stuff 1')
pd.set_option('max_colwidth', curr_max_colwidth)

def test_unicode_string_with_unicode(self):
df = DataFrame({'A': [u("\u05d0")]})

Expand Down