Skip to content

BUG: Fix categorical formatter not folding the output when it exceeds display width #17639

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ Categorical
- Bug in :func:`Series.isin` when called with a categorical (:issue`16639`)
- Bug in the categorical constructor with empty values and categories causing the ``.categories`` to be an empty ``Float64Index`` rather than an empty ``Index`` with object dtype (:issue:`17248`)
- Bug in categorical operations with :ref:`Series.cat <categorical.cat>' not preserving the original Series' name (:issue:`17509`)
- Bug in categorical formatter not folding the output when it exceeds display width (:issue:`12066`)

PyPy
^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ def _tidy_repr(self, max_vals=10, footer=True):
head = self[:num]._get_repr(length=False, footer=False)
tail = self[-(max_vals - num):]._get_repr(length=False, footer=False)

result = '%s, ..., %s' % (head[:-1], tail[1:])
result = '%s,\n ...\n %s' % (head[:-1], tail[1:])
if footer:
result = '%s\n%s' % (result, self._repr_footer())

Expand Down
100 changes: 96 additions & 4 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,100 @@ def _get_formatted_values(self):
return format_array(self.categorical.get_values(), None,
float_format=None, na_rep=self.na_rep)

def _format_data(self, values):
"""
Return the formatted data as a unicode string
"""
from pandas.io.formats.console import get_console_size
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you copying this here?

Copy link
Author

Choose a reason for hiding this comment

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

because i didn't think up a good idea to reuse Index#_format_data() in base.py. (and the output is slightly different.)
any idea?

from pandas.io.formats.format import _get_adjustment
display_width, _ = get_console_size()
if display_width is None:
display_width = get_option('display.width') or 80

space = "\n "

n = len(values)
sep = ','
max_seq_items = get_option('display.max_seq_items') or n

# are we a truncated display
is_truncated = n > max_seq_items

# adj can optionaly handle unicode eastern asian width
adj = _get_adjustment()

def _extend_line(s, line, value, display_width, next_line_prefix):

if (adj.len(line.rstrip()) + adj.len(value.rstrip()) >=
display_width):
s += line.rstrip()
line = next_line_prefix
line += value
return s, line

def best_len(values):
if values:
return max([adj.len(x) for x in values])
else:
return 0

if n == 0:
summary = '[]'
elif n == 1:
first = values[0].strip('\'')
summary = '[%s]' % first
elif n == 2:
first = values[0].strip('\'')
last = values[-1].strip('\'')
summary = '[%s, %s]' % (first, last)
else:

if n > max_seq_items:
n = min(max_seq_items // 2, 10)
head = [x.strip('\'') for x in values[:n]]
tail = [x.strip('\'') for x in values[-n:]]
else:
head = []
tail = [x.strip('\'') for x in values]

# however, if we are not truncated and we are only a single
# line, then don't justify
if (is_truncated or
not (len(', '.join(head)) < display_width and
len(', '.join(tail)) < display_width)):
max_len = max(best_len(head), best_len(tail))
head = [x.rjust(max_len) for x in head]
tail = [x.rjust(max_len) for x in tail]

summary = ""
line = space

for i in range(len(head)):
word = head[i] + sep + ' '
summary, line = _extend_line(summary, line, word,
display_width, space)

if is_truncated:
# remove trailing space of last line
summary += line.rstrip() + space + '...'
line = space

for i in range(len(tail) - 1):
word = tail[i] + sep + ' '
summary, line = _extend_line(summary, line, word,
display_width, space)

# last value: no sep added + 1 space of width used for trailing ','
summary, line = _extend_line(summary, line, tail[-1],
display_width - 2, space)
summary += line
summary += ']'

# remove initial space
summary = '[' + summary[len(space):]

return summary

def to_string(self):
categorical = self.categorical

Expand All @@ -136,10 +230,8 @@ def to_string(self):

fmt_values = self._get_formatted_values()

result = [u('{i}').format(i=i) for i in fmt_values]
result = [i.strip() for i in result]
result = u(', ').join(result)
result = [u('[') + result + u(']')]
fmt_values = [i.strip() for i in fmt_values]
result = [self._format_data(fmt_values)]
if self.footer:
footer = self._get_footer()
if footer:
Expand Down
Loading