Skip to content

Commit 27b0ba7

Browse files
jdclajorisvandenbossche
authored andcommitted
DOC: add documentation to IndexSlice (#15623)
1 parent ae0a92a commit 27b0ba7

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,7 @@ MultiIndex
14051405
:toctree: generated/
14061406

14071407
MultiIndex
1408+
IndexSlice
14081409

14091410
MultiIndex Components
14101411
~~~~~~~~~~~~~~~~~~~~~~

pandas/core/indexing.py

+30
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,36 @@ def get_indexers_list():
4343

4444
# the public IndexSlicerMaker
4545
class _IndexSlice(object):
46+
"""
47+
Create an object to more easily perform multi-index slicing
48+
49+
Examples
50+
--------
51+
52+
>>> midx = pd.MultiIndex.from_product([['A0','A1'], ['B0','B1','B2','B3']])
53+
>>> columns = ['foo', 'bar']
54+
>>> dfmi = pd.DataFrame(np.arange(16).reshape((len(midx), len(columns))),
55+
index=midx, columns=columns)
56+
57+
Using the default slice command:
58+
59+
>>> dfmi.loc[(slice(None), slice('B0', 'B1')), :]
60+
foo bar
61+
A0 B0 0 1
62+
B1 2 3
63+
A1 B0 8 9
64+
B1 10 11
65+
66+
Using the IndexSlice class for a more intuitive command:
67+
68+
>>> idx = pd.IndexSlice
69+
>>> dfmi.loc[idx[:, 'B0':'B1'], :]
70+
foo bar
71+
A0 B0 0 1
72+
B1 2 3
73+
A1 B0 8 9
74+
B1 10 11
75+
"""
4676

4777
def __getitem__(self, arg):
4878
return arg

0 commit comments

Comments
 (0)