Skip to content

Commit 1454862

Browse files
author
tp
committed
improve MultiIndex repr str (pandas-dev#12423)
1 parent bc37ea2 commit 1454862

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

pandas/core/indexes/multi.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,28 @@ def _format_attrs(self):
609609
"""
610610
Return a list of tuples of the (attr,formatted_value)
611611
"""
612+
def to_string_helper(obj, attr_name):
613+
"""converts obj.attr_name to a string.
614+
"""
615+
indices = getattr(obj, attr_name)
616+
if attr_name == 'labels':
617+
# self.labels is a list of FrozenNDArray, Index._format_data
618+
# expects a pd.Index
619+
indices = [Index(i) for i in indices]
620+
621+
_name = "{}({}=".format(obj.__class__.__name__, attr_name)
622+
attr_string = [idx._format_data(name=_name)
623+
for idx in indices]
624+
attr_string = "".join(attr_string)
625+
if attr_string.endswith(", "): # else output [1, 2, ], want [1, 2]
626+
attr_string = attr_string[:-2]
627+
628+
return "[{}]".format(attr_string)
629+
612630
attrs = [
613-
('levels', ibase.default_pprint(self._levels,
614-
max_seq_items=False)),
615-
('labels', ibase.default_pprint(self._labels,
616-
max_seq_items=False))]
631+
('levels', to_string_helper(self, attr_name='levels')),
632+
('labels', to_string_helper(self, attr_name='labels')),
633+
]
617634
if com._any_not_none(*self.names):
618635
attrs.append(('names', ibase.default_pprint(self.names)))
619636
if self.sortorder is not None:

0 commit comments

Comments
 (0)