Skip to content

Commit 64b66a7

Browse files
committed
Improve docs
1 parent 732af8c commit 64b66a7

File tree

4 files changed

+69
-21
lines changed

4 files changed

+69
-21
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ These are the changes in pandas 0.25.0. See :ref:`release` for a full changelog
1919
including other versions of pandas.
2020

2121

22+
.. _whatsnew_0240.enhancements.multi_index_repr:
23+
24+
Better repr for MultiIndex
25+
^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
Printing of :class:`MultiIndex` instances now shows tuples of each row and ensures
28+
that the tuple items are vertically aligned, so it's now easier to understand
29+
the structure of the ``MultiIndex``. (:issue:`13480`):
30+
31+
The repr now looks like this:
32+
33+
.. ipython:: python
34+
35+
pd.MultiIndex.from_product([['a', 'abc'], range(500)])
36+
37+
Previously, outputting a :class:`MultiIndex` printed all the ``levels`` and
38+
``labels`` of the ``MultiIndex``, which was visually unappealing and made
39+
the output more difficult to navigate:
40+
41+
.. code-block:: ipython
42+
43+
>>>pd.MultiIndex.from_product([['a', 'abc'], range(5)])
44+
MultiIndex(levels=[['a', 'abc'], [0, 1, 2, 3]],
45+
labels=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])
46+
47+
In the new repr, all values will be shown, if the number of rows is smaller
48+
than :attr:`options.display.max_seq_items` (default: 100 items). Horizontally,
49+
the output will truncate, if it's wider than :attr:`options.display.width`
50+
(default: 80 characters).
51+
52+
2253
.. _whatsnew_0250.enhancements.other:
2354

2455
Other Enhancements

pandas/core/indexes/base.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,17 +1302,24 @@ def set_names(self, names, level=None, inplace=False):
13021302
>>> idx = pd.MultiIndex.from_product([['python', 'cobra'],
13031303
... [2018, 2019]])
13041304
>>> idx
1305-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1306-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]])
1305+
MultiIndex([('python', 2018),
1306+
('python', 2019),
1307+
( 'cobra', 2018),
1308+
( 'cobra', 2019)],
1309+
dtype='object')
13071310
>>> idx.set_names(['kind', 'year'], inplace=True)
13081311
>>> idx
1309-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1310-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1311-
names=['kind', 'year'])
1312+
MultiIndex([('python', 2018),
1313+
('python', 2019),
1314+
( 'cobra', 2018),
1315+
( 'cobra', 2019)],
1316+
dtype='object', names=['kind', 'year'])
13121317
>>> idx.set_names('species', level=0)
1313-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1314-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1315-
names=['species', 'year'])
1318+
MultiIndex([('python', 2018),
1319+
('python', 2019),
1320+
( 'cobra', 2018),
1321+
( 'cobra', 2019)],
1322+
dtype='object', names=['species', 'year'])
13161323
"""
13171324

13181325
if level is not None and not isinstance(self, ABCMultiIndex):
@@ -1373,13 +1380,17 @@ def rename(self, name, inplace=False):
13731380
... [2018, 2019]],
13741381
... names=['kind', 'year'])
13751382
>>> idx
1376-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1377-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1378-
names=['kind', 'year'])
1383+
MultiIndex([('python', 2018),
1384+
('python', 2019),
1385+
( 'cobra', 2018),
1386+
( 'cobra', 2019)],
1387+
dtype='object', names=['kind', 'year'])
13791388
>>> idx.rename(['species', 'year'])
1380-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1381-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1382-
names=['species', 'year'])
1389+
MultiIndex([('python', 2018),
1390+
('python', 2019),
1391+
( 'cobra', 2018),
1392+
( 'cobra', 2019)],
1393+
dtype='object', names=['species', 'year'])
13831394
>>> idx.rename('species')
13841395
Traceback (most recent call last):
13851396
TypeError: Must pass list-like as `names`.
@@ -5298,9 +5309,9 @@ def ensure_index_from_sequences(sequences, names=None):
52985309
52995310
>>> ensure_index_from_sequences([['a', 'a'], ['a', 'b']],
53005311
names=['L1', 'L2'])
5301-
MultiIndex(levels=[['a'], ['a', 'b']],
5302-
codes=[[0, 0], [0, 1]],
5303-
names=['L1', 'L2'])
5312+
MultiIndex([('a', 'a'),
5313+
('a', 'b')],
5314+
dtype='object', names=['L1', 'L2'])
53045315
53055316
See Also
53065317
--------
@@ -5339,8 +5350,14 @@ def ensure_index(index_like, copy=False):
53395350
Index([('a', 'a'), ('b', 'c')], dtype='object')
53405351
53415352
>>> ensure_index([['a', 'a'], ['b', 'c']])
5353+
<<<<<<< HEAD
53425354
MultiIndex(levels=[['a'], ['b', 'c']],
53435355
codes=[[0, 0], [0, 1]])
5356+
=======
5357+
MultiIndex([('a', 'b'),
5358+
('a', 'c')],
5359+
dtype='object')
5360+
>>>>>>> Improve docs
53445361
53455362
See Also
53465363
--------

pandas/core/indexes/multi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
from pandas.core.indexes.frozen import FrozenList, _ensure_frozen
3131
import pandas.core.missing as missing
3232

33-
from pandas.io.formats.printing import (
34-
default_pprint, format_object_summary, pprint_thing)
33+
from pandas.io.formats.printing import format_object_summary, pprint_thing
3534

3635
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
3736
_index_doc_kwargs.update(

pandas/core/strings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,8 +2460,9 @@ def rsplit(self, pat=None, n=-1, expand=False):
24602460
Which will create a MultiIndex:
24612461
24622462
>>> idx.str.partition()
2463-
MultiIndex(levels=[['X', 'Y'], [' '], ['123', '999']],
2464-
codes=[[0, 1], [0, 0], [0, 1]])
2463+
MultiIndex([('X', ' ', '123'),
2464+
('Y', ' ', '999')],
2465+
dtype='object')
24652466
24662467
Or an index with tuples with ``expand=False``:
24672468

0 commit comments

Comments
 (0)