Skip to content

Commit

Permalink
improve MultiIndex repr str (#12423)
Browse files Browse the repository at this point in the history
  • Loading branch information
tp committed May 20, 2018
1 parent bc37ea2 commit 4fd6884
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,28 @@ def _format_attrs(self):
"""
Return a list of tuples of the (attr,formatted_value)
"""
def to_string_helper(obj, attr_name):
"""converts obj.attr_name to a string.
"""
indices = getattr(obj, attr_name)
if attr_name == 'labels':
# self.labels is a list of FrozenNDArray, Index._format_data
# expects a pd.Index
indices = [Index(i) for i in indices]

_name = u"{}({}=".format(obj.__class__.__name__, attr_name)
attr_string = [idx._format_data(name=_name)
for idx in indices]
attr_string = u"".join(attr_string)
if attr_string.endswith(u", "): # else output [1, 2, ], want [1, 2]
attr_string = attr_string[:-2]

return u"[{}]".format(attr_string)

attrs = [
('levels', ibase.default_pprint(self._levels,
max_seq_items=False)),
('labels', ibase.default_pprint(self._labels,
max_seq_items=False))]
('levels', to_string_helper(self, attr_name='levels')),
('labels', to_string_helper(self, attr_name='labels')),
]
if com._any_not_none(*self.names):
attrs.append(('names', ibase.default_pprint(self.names)))
if self.sortorder is not None:
Expand Down

0 comments on commit 4fd6884

Please sign in to comment.