Skip to content

Commit bbde4ad

Browse files
committed
Improve docs
1 parent e395e74 commit bbde4ad

File tree

4 files changed

+69
-21
lines changed

4 files changed

+69
-21
lines changed

doc/source/whatsnew/v0.25.0.rst

+31
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

+34-17
Original file line numberDiff line numberDiff line change
@@ -1308,17 +1308,24 @@ def set_names(self, names, level=None, inplace=False):
13081308
>>> idx = pd.MultiIndex.from_product([['python', 'cobra'],
13091309
... [2018, 2019]])
13101310
>>> idx
1311-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1312-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]])
1311+
MultiIndex([('python', 2018),
1312+
('python', 2019),
1313+
( 'cobra', 2018),
1314+
( 'cobra', 2019)],
1315+
dtype='object')
13131316
>>> idx.set_names(['kind', 'year'], inplace=True)
13141317
>>> idx
1315-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1316-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1317-
names=['kind', 'year'])
1318+
MultiIndex([('python', 2018),
1319+
('python', 2019),
1320+
( 'cobra', 2018),
1321+
( 'cobra', 2019)],
1322+
dtype='object', names=['kind', 'year'])
13181323
>>> idx.set_names('species', level=0)
1319-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1320-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1321-
names=['species', 'year'])
1324+
MultiIndex([('python', 2018),
1325+
('python', 2019),
1326+
( 'cobra', 2018),
1327+
( 'cobra', 2019)],
1328+
dtype='object', names=['species', 'year'])
13221329
"""
13231330

13241331
if level is not None and not isinstance(self, ABCMultiIndex):
@@ -1379,13 +1386,17 @@ def rename(self, name, inplace=False):
13791386
... [2018, 2019]],
13801387
... names=['kind', 'year'])
13811388
>>> idx
1382-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1383-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1384-
names=['kind', 'year'])
1389+
MultiIndex([('python', 2018),
1390+
('python', 2019),
1391+
( 'cobra', 2018),
1392+
( 'cobra', 2019)],
1393+
dtype='object', names=['kind', 'year'])
13851394
>>> idx.rename(['species', 'year'])
1386-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1387-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1388-
names=['species', 'year'])
1395+
MultiIndex([('python', 2018),
1396+
('python', 2019),
1397+
( 'cobra', 2018),
1398+
( 'cobra', 2019)],
1399+
dtype='object', names=['species', 'year'])
13891400
>>> idx.rename('species')
13901401
Traceback (most recent call last):
13911402
TypeError: Must pass list-like as `names`.
@@ -5313,9 +5324,9 @@ def ensure_index_from_sequences(sequences, names=None):
53135324
53145325
>>> ensure_index_from_sequences([['a', 'a'], ['a', 'b']],
53155326
names=['L1', 'L2'])
5316-
MultiIndex(levels=[['a'], ['a', 'b']],
5317-
codes=[[0, 0], [0, 1]],
5318-
names=['L1', 'L2'])
5327+
MultiIndex([('a', 'a'),
5328+
('a', 'b')],
5329+
dtype='object', names=['L1', 'L2'])
53195330
53205331
See Also
53215332
--------
@@ -5354,8 +5365,14 @@ def ensure_index(index_like, copy=False):
53545365
Index([('a', 'a'), ('b', 'c')], dtype='object')
53555366
53565367
>>> ensure_index([['a', 'a'], ['b', 'c']])
5368+
<<<<<<< HEAD
53575369
MultiIndex(levels=[['a'], ['b', 'c']],
53585370
codes=[[0, 0], [0, 1]])
5371+
=======
5372+
MultiIndex([('a', 'b'),
5373+
('a', 'c')],
5374+
dtype='object')
5375+
>>>>>>> Improve docs
53595376
53605377
See Also
53615378
--------

pandas/core/indexes/multi.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
from pandas.core.indexes.frozen import FrozenList, _ensure_frozen
3333
import pandas.core.missing as missing
3434

35-
from pandas.io.formats.printing import (
36-
default_pprint, format_object_summary, pprint_thing)
35+
from pandas.io.formats.printing import format_object_summary, pprint_thing
3736

3837
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
3938
_index_doc_kwargs.update(

pandas/core/strings.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2481,8 +2481,9 @@ def rsplit(self, pat=None, n=-1, expand=False):
24812481
Which will create a MultiIndex:
24822482
24832483
>>> idx.str.partition()
2484-
MultiIndex(levels=[['X', 'Y'], [' '], ['123', '999']],
2485-
codes=[[0, 1], [0, 0], [0, 1]])
2484+
MultiIndex([('X', ' ', '123'),
2485+
('Y', ' ', '999')],
2486+
dtype='object')
24862487
24872488
Or an index with tuples with ``expand=False``:
24882489

0 commit comments

Comments
 (0)