Skip to content

Commit e822b77

Browse files
author
tp
committed
improve MultiIndex repr str (#12423)
1 parent bc37ea2 commit e822b77

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

pandas/core/indexes/multi.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88
from pandas._libs import algos as libalgos, index as libindex, lib, Timestamp
99

10-
from pandas.compat import range, zip, lrange, lzip, map
10+
from pandas.compat import range, zip, lrange, lzip, map, u
1111
from pandas.compat.numpy import function as nv
1212
from pandas import compat
1313

@@ -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 = u("{}({}=").format(obj.__class__.__name__, attr_name)
622+
attr_string = [idx._format_data(name=_name)
623+
for idx in indices]
624+
attr_string = u("").join(attr_string)
625+
if attr_string.endswith(u(", ")): # else [1, 2, ], want [1, 2]
626+
attr_string = attr_string[:-2]
627+
628+
return u("[{}]").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)