From d7bcb227f7b99b66643dbc1e858c4ba7170c3f5c Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Sat, 10 Mar 2018 15:49:31 +0100 Subject: [PATCH] DOC: update the MultiIndex.swaplevel docstring (#20105) --- pandas/core/indexes/multi.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 8b6d945854960..be64f6f4bfd0f 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1775,22 +1775,45 @@ def droplevel(self, level=0): def swaplevel(self, i=-2, j=-1): """ - Swap level i with level j. Do not change the ordering of anything + Swap level i with level j. + + Calling this method does not change the ordering of the values. Parameters ---------- - i, j : int, string (can be mixed) - Level of index to be swapped. Can pass level name as string. + i : int, str, default -2 + First level of index to be swapped. Can pass level name as string. + Type of parameters can be mixed. + j : int, str, default -1 + Second level of index to be swapped. Can pass level name as string. + Type of parameters can be mixed. Returns ------- - swapped : MultiIndex + MultiIndex + A new MultiIndex .. versionchanged:: 0.18.1 The indexes ``i`` and ``j`` are now optional, and default to the two innermost levels of the index. + See Also + -------- + Series.swaplevel : Swap levels i and j in a MultiIndex + Dataframe.swaplevel : Swap levels i and j in a MultiIndex on a + particular axis + + Examples + -------- + >>> mi = pd.MultiIndex(levels=[['a', 'b'], ['bb', 'aa']], + ... labels=[[0, 0, 1, 1], [0, 1, 0, 1]]) + >>> mi + MultiIndex(levels=[['a', 'b'], ['bb', 'aa']], + labels=[[0, 0, 1, 1], [0, 1, 0, 1]]) + >>> mi.swaplevel(0, 1) + MultiIndex(levels=[['bb', 'aa'], ['a', 'b']], + labels=[[0, 1, 0, 1], [0, 0, 1, 1]]) """ new_levels = list(self.levels) new_labels = list(self.labels)