Skip to content

Commit cb2f904

Browse files
committed
Improve docs
1 parent 7a8512e commit cb2f904

File tree

4 files changed

+67
-42
lines changed

4 files changed

+67
-42
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,38 @@ a dict to a Series groupby aggregation (:ref:`whatsnew_0200.api_breaking.depreca
7474

7575
See :ref:`_groupby.aggregate.named` for more.
7676

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

79111
Other Enhancements

pandas/core/indexes/base.py

Lines changed: 31 additions & 19 deletions
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`.
@@ -5420,9 +5431,9 @@ def ensure_index_from_sequences(sequences, names=None):
54205431
54215432
>>> ensure_index_from_sequences([['a', 'a'], ['a', 'b']],
54225433
names=['L1', 'L2'])
5423-
MultiIndex(levels=[['a'], ['a', 'b']],
5424-
codes=[[0, 0], [0, 1]],
5425-
names=['L1', 'L2'])
5434+
MultiIndex([('a', 'a'),
5435+
('a', 'b')],
5436+
dtype='object', names=['L1', 'L2'])
54265437
54275438
See Also
54285439
--------
@@ -5461,8 +5472,9 @@ def ensure_index(index_like, copy=False):
54615472
Index([('a', 'a'), ('b', 'c')], dtype='object')
54625473
54635474
>>> ensure_index([['a', 'a'], ['b', 'c']])
5464-
MultiIndex(levels=[['a'], ['b', 'c']],
5465-
codes=[[0, 0], [0, 1]])
5475+
MultiIndex([('a', 'b'),
5476+
('a', 'c')],
5477+
dtype='object')
54665478
54675479
See Also
54685480
--------

pandas/core/indexes/multi.py

Lines changed: 1 addition & 21 deletions
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(
@@ -816,24 +815,6 @@ def set_codes(self, codes, level=None, inplace=False,
816815
>>> idx = pd.MultiIndex.from_tuples([(1, 'one'), (1, 'two'),
817816
(2, 'one'), (2, 'two')],
818817
names=['foo', 'bar'])
819-
<<<<<<< HEAD
820-
>>> idx.set_codes([[1,0,1,0], [0,0,1,1]])
821-
MultiIndex(levels=[[1, 2], ['one', 'two']],
822-
codes=[[1, 0, 1, 0], [0, 0, 1, 1]],
823-
names=['foo', 'bar'])
824-
>>> idx.set_codes([1,0,1,0], level=0)
825-
MultiIndex(levels=[[1, 2], ['one', 'two']],
826-
codes=[[1, 0, 1, 0], [0, 1, 0, 1]],
827-
names=['foo', 'bar'])
828-
>>> idx.set_codes([0,0,1,1], level='bar')
829-
MultiIndex(levels=[[1, 2], ['one', 'two']],
830-
codes=[[0, 0, 1, 1], [0, 0, 1, 1]],
831-
names=['foo', 'bar'])
832-
>>> idx.set_codes([[1,0,1,0], [0,0,1,1]], level=[0,1])
833-
MultiIndex(levels=[[1, 2], ['one', 'two']],
834-
codes=[[1, 0, 1, 0], [0, 0, 1, 1]],
835-
names=['foo', 'bar'])
836-
=======
837818
>>> idx.set_codes([[1, 0, 1, 0], [0, 0, 1, 1]])
838819
MultiIndex([(2, 'one'),
839820
(1, 'one'),
@@ -858,7 +839,6 @@ def set_codes(self, codes, level=None, inplace=False,
858839
(2, 'two'),
859840
(1, 'two')],
860841
dtype='object', names=['foo', 'bar'])
861-
>>>>>>> Update doc string examples and docs
862842
"""
863843
if level is not None and not is_list_like(level):
864844
if not is_list_like(codes):

pandas/core/strings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,8 +2548,9 @@ def rsplit(self, pat=None, n=-1, expand=False):
25482548
Which will create a MultiIndex:
25492549
25502550
>>> idx.str.partition()
2551-
MultiIndex(levels=[['X', 'Y'], [' '], ['123', '999']],
2552-
codes=[[0, 1], [0, 0], [0, 1]])
2551+
MultiIndex([('X', ' ', '123'),
2552+
('Y', ' ', '999')],
2553+
dtype='object')
25532554
25542555
Or an index with tuples with ``expand=False``:
25552556

0 commit comments

Comments
 (0)