Skip to content

Commit 590027e

Browse files
committed
add doc example
1 parent 2dec73f commit 590027e

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

doc/source/whatsnew/v0.20.0.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ Other Enhancements
365365
- ``pandas.io.json.json_normalize()`` gained the option ``errors='ignore'|'raise'``; the default is ``errors='raise'`` which is backward compatible. (:issue:`14583`)
366366
- ``pandas.io.json.json_normalize()`` with an empty ``list`` will return an empty ``DataFrame`` (:issue:`15534`)
367367
- ``pandas.io.json.json_normalize()`` has gained a ``sep`` option that accepts ``str`` to separate joined fields; the default is ".", which is backward compatible. (:issue:`14883`)
368-
- A new function has been added to a ``MultiIndex`` to facilitate :ref:`Removing Unused Levels <advanced.shown_levels>`. (:issue:`15694`)
368+
- :func:`MultiIndex.remove_unused_levels` has been added to facilitate :ref:`removing unused levels <advanced.shown_levels>`. (:issue:`15694`)
369369

370370

371371
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations
@@ -745,12 +745,14 @@ Sorting works as expected
745745
df.sort_index().index.is_lexsorted()
746746
df.sort_index().index.is_monotonic
747747

748-
However, this example, which has a monotonic level, doesn't behave as desired.
748+
However, this example, which has a non-monotonic 2nd level,
749+
doesn't behave as desired.
749750

750751
.. ipython:: python
751-
df = pd.DataFrame({'value': [1, 2, 3, 4]},
752-
index=pd.MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
753-
labels=[[0, 0, 1, 1], [0, 1, 0, 1]]))
752+
df = pd.DataFrame(
753+
{'value': [1, 2, 3, 4]},
754+
index=pd.MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
755+
labels=[[0, 0, 1, 1], [0, 1, 0, 1]]))
754756

755757
Previous Behavior:
756758

pandas/indexes/multi.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,23 @@ def remove_unused_levels(self):
12351235
-------
12361236
MultiIndex
12371237
1238+
Examples
1239+
--------
1240+
>>> i = MultiIndex.from_product([range(2), list('ab')])
1241+
MultiIndex(levels=[[0, 1], ['a', 'b']],
1242+
labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
1243+
1244+
1245+
>>> i[2:]
1246+
MultiIndex(levels=[[0, 1], ['a', 'b']],
1247+
labels=[[1, 1], [0, 1]])
1248+
1249+
# the 0 from the first level is not represented
1250+
# and can be removed
1251+
>>> i[2:].remove_unused_levels()
1252+
MultiIndex(levels=[[1], ['a', 'b']],
1253+
labels=[[0, 0], [0, 1]])
1254+
12381255
"""
12391256

12401257
new_levels = []

0 commit comments

Comments
 (0)