Skip to content

Change MultiIndex repr ? #13480

Closed
Closed
@jorisvandenbossche

Description

@jorisvandenbossche

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:

  • The shown level and labels are actually (internal) concepts of MultiIndex a lot of user do not have to think about.
  • It also does not give you the best idea how the MultiIndex looks like at a glance IMO (since you have to look at the values of the labels to know what the order of the levels will be).

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    MultiIndexNeeds DiscussionRequires discussion from core team before further actionOutput-Formatting__repr__ of pandas objects, to_string

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions