Skip to content

Commit 33c44bb

Browse files
committed
Improve docs
1 parent 3895633 commit 33c44bb

File tree

4 files changed

+67
-42
lines changed

4 files changed

+67
-42
lines changed

doc/source/whatsnew/v0.25.0.rst

+32
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ approach to naming the output of column-specific aggregations (:ref:`whatsnew_02
6060

6161
See :ref:`_groupby.aggregate.named` for more.
6262

63+
64+
.. _whatsnew_0240.enhancements.multi_index_repr:
65+
66+
Better repr for MultiIndex
67+
^^^^^^^^^^^^^^^^^^^^^^^^^^
68+
69+
Printing of :class:`MultiIndex` instances now shows tuples of each row and ensures
70+
that the tuple items are vertically aligned, so it's now easier to understand
71+
the structure of the ``MultiIndex``. (:issue:`13480`):
72+
73+
The repr now looks like this:
74+
75+
.. ipython:: python
76+
77+
pd.MultiIndex.from_product([['a', 'abc'], range(500)])
78+
79+
Previously, outputting a :class:`MultiIndex` printed all the ``levels`` and
80+
``labels`` of the ``MultiIndex``, which was visually unappealing and made
81+
the output more difficult to navigate:
82+
83+
.. code-block:: ipython
84+
85+
>>>pd.MultiIndex.from_product([['a', 'abc'], range(5)])
86+
MultiIndex(levels=[['a', 'abc'], [0, 1, 2, 3]],
87+
labels=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])
88+
89+
In the new repr, all values will be shown, if the number of rows is smaller
90+
than :attr:`options.display.max_seq_items` (default: 100 items). Horizontally,
91+
the output will truncate, if it's wider than :attr:`options.display.width`
92+
(default: 80 characters).
93+
94+
6395
.. _whatsnew_0250.enhancements.other:
6496

6597
Other Enhancements

pandas/core/indexes/base.py

+31-19
Original file line numberDiff line numberDiff line change
@@ -1322,17 +1322,24 @@ def set_names(self, names, level=None, inplace=False):
13221322
>>> idx = pd.MultiIndex.from_product([['python', 'cobra'],
13231323
... [2018, 2019]])
13241324
>>> idx
1325-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1326-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]])
1325+
MultiIndex([('python', 2018),
1326+
('python', 2019),
1327+
( 'cobra', 2018),
1328+
( 'cobra', 2019)],
1329+
dtype='object')
13271330
>>> idx.set_names(['kind', 'year'], inplace=True)
13281331
>>> idx
1329-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1330-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1331-
names=['kind', 'year'])
1332+
MultiIndex([('python', 2018),
1333+
('python', 2019),
1334+
( 'cobra', 2018),
1335+
( 'cobra', 2019)],
1336+
dtype='object', names=['kind', 'year'])
13321337
>>> idx.set_names('species', level=0)
1333-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1334-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1335-
names=['species', 'year'])
1338+
MultiIndex([('python', 2018),
1339+
('python', 2019),
1340+
( 'cobra', 2018),
1341+
( 'cobra', 2019)],
1342+
dtype='object', names=['species', 'year'])
13361343
"""
13371344

13381345
if level is not None and not isinstance(self, ABCMultiIndex):
@@ -1393,13 +1400,17 @@ def rename(self, name, inplace=False):
13931400
... [2018, 2019]],
13941401
... names=['kind', 'year'])
13951402
>>> idx
1396-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1397-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1398-
names=['kind', 'year'])
1403+
MultiIndex([('python', 2018),
1404+
('python', 2019),
1405+
( 'cobra', 2018),
1406+
( 'cobra', 2019)],
1407+
dtype='object', names=['kind', 'year'])
13991408
>>> idx.rename(['species', 'year'])
1400-
MultiIndex(levels=[['cobra', 'python'], [2018, 2019]],
1401-
codes=[[1, 1, 0, 0], [0, 1, 0, 1]],
1402-
names=['species', 'year'])
1409+
MultiIndex([('python', 2018),
1410+
('python', 2019),
1411+
( 'cobra', 2018),
1412+
( 'cobra', 2019)],
1413+
dtype='object', names=['species', 'year'])
14031414
>>> idx.rename('species')
14041415
Traceback (most recent call last):
14051416
TypeError: Must pass list-like as `names`.
@@ -5422,9 +5433,9 @@ def ensure_index_from_sequences(sequences, names=None):
54225433
54235434
>>> ensure_index_from_sequences([['a', 'a'], ['a', 'b']],
54245435
names=['L1', 'L2'])
5425-
MultiIndex(levels=[['a'], ['a', 'b']],
5426-
codes=[[0, 0], [0, 1]],
5427-
names=['L1', 'L2'])
5436+
MultiIndex([('a', 'a'),
5437+
('a', 'b')],
5438+
dtype='object', names=['L1', 'L2'])
54285439
54295440
See Also
54305441
--------
@@ -5463,8 +5474,9 @@ def ensure_index(index_like, copy=False):
54635474
Index([('a', 'a'), ('b', 'c')], dtype='object')
54645475
54655476
>>> ensure_index([['a', 'a'], ['b', 'c']])
5466-
MultiIndex(levels=[['a'], ['b', 'c']],
5467-
codes=[[0, 0], [0, 1]])
5477+
MultiIndex([('a', 'b'),
5478+
('a', 'c')],
5479+
dtype='object')
54685480
54695481
See Also
54705482
--------

pandas/core/indexes/multi.py

+1-21
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
from pandas.core.indexes.frozen import FrozenList, _ensure_frozen
3030
import pandas.core.missing as missing
3131

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

3534
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
3635
_index_doc_kwargs.update(
@@ -774,24 +773,6 @@ def set_codes(self, codes, level=None, inplace=False,
774773
>>> idx = pd.MultiIndex.from_tuples([(1, 'one'), (1, 'two'),
775774
(2, 'one'), (2, 'two')],
776775
names=['foo', 'bar'])
777-
<<<<<<< HEAD
778-
>>> idx.set_codes([[1,0,1,0], [0,0,1,1]])
779-
MultiIndex(levels=[[1, 2], ['one', 'two']],
780-
codes=[[1, 0, 1, 0], [0, 0, 1, 1]],
781-
names=['foo', 'bar'])
782-
>>> idx.set_codes([1,0,1,0], level=0)
783-
MultiIndex(levels=[[1, 2], ['one', 'two']],
784-
codes=[[1, 0, 1, 0], [0, 1, 0, 1]],
785-
names=['foo', 'bar'])
786-
>>> idx.set_codes([0,0,1,1], level='bar')
787-
MultiIndex(levels=[[1, 2], ['one', 'two']],
788-
codes=[[0, 0, 1, 1], [0, 0, 1, 1]],
789-
names=['foo', 'bar'])
790-
>>> idx.set_codes([[1,0,1,0], [0,0,1,1]], level=[0,1])
791-
MultiIndex(levels=[[1, 2], ['one', 'two']],
792-
codes=[[1, 0, 1, 0], [0, 0, 1, 1]],
793-
names=['foo', 'bar'])
794-
=======
795776
>>> idx.set_codes([[1, 0, 1, 0], [0, 0, 1, 1]])
796777
MultiIndex([(2, 'one'),
797778
(1, 'one'),
@@ -816,7 +797,6 @@ def set_codes(self, codes, level=None, inplace=False,
816797
(2, 'two'),
817798
(1, 'two')],
818799
dtype='object', names=['foo', 'bar'])
819-
>>>>>>> Update doc string examples and docs
820800
"""
821801
if level is not None and not is_list_like(level):
822802
if not is_list_like(codes):

pandas/core/strings.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2468,8 +2468,9 @@ def rsplit(self, pat=None, n=-1, expand=False):
24682468
Which will create a MultiIndex:
24692469
24702470
>>> idx.str.partition()
2471-
MultiIndex(levels=[['X', 'Y'], [' '], ['123', '999']],
2472-
codes=[[0, 1], [0, 0], [0, 1]])
2471+
MultiIndex([('X', ' ', '123'),
2472+
('Y', ' ', '999')],
2473+
dtype='object')
24732474
24742475
Or an index with tuples with ``expand=False``:
24752476

0 commit comments

Comments
 (0)