Change MultiIndex repr ? · Issue #13480 · pandas-dev/pandas (original) (raw)

From #13443 (comment)

The current MultiIndex representation looks like this:

In [15]: mi = pd.MultiIndex.from_tuples([('A', 1), ('A', 2),
    ...:                                 ('B', 3), ('B', 4)])

In [16]: mi
Out[16]:
MultiIndex(levels=[[u'A', u'B'], [1, 2, 3, 4]],
           labels=[[0, 0, 1, 1], [0, 1, 2, 3]])

So this shows the underlying labels and levels. Personally, I don't find this a very good repr, because:

So the question is: is there a better alternative?

We could show tuples:

MultiIndex([('A', 1), ('A', 2), ('B', 3), ('B', 4)])

Or the individual levels:

MultiIndex([['A', 'A', 'B', 'B'], [1, 2, 3, 4]])

or ..

Historical note: in older versions, there was a difference between the repr and str of a MultiIndex:

In [1]: mi = pd.MultiIndex.from_tuples([('A', 1), ('A', 2),
   ...:                                         ('B', 3), ('B', 4)])

In [2]: mi
Out[2]:
MultiIndex(levels=[[u'A', u'B'], [1, 2, 3, 4]],
           labels=[[0, 0, 1, 1], [0, 1, 2, 3]])

In [3]: print mi

A  1
   2
B  3
   4

In [6]: pd.__version__
Out[6]: '0.14.1'

but seems to been disappeared (on purpose or not).