Skip to content

Commit 48249ab

Browse files
committed
add doc example
1 parent 527c3a6 commit 48249ab

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

doc/source/whatsnew/v0.20.0.txt

+7-4
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ Other Enhancements
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`)
368368
- ``pd.read_csv()`` will now raise a ``csv.Error`` error whenever an end-of-file character is encountered in the middle of a data row (:issue:`15913`)
369369
- A new function has been added to a ``MultiIndex`` to facilitate :ref:`Removing Unused Levels <advanced.shown_levels>`. (:issue:`15694`)
370+
- :func:`MultiIndex.remove_unused_levels` has been added to facilitate :ref:`removing unused levels <advanced.shown_levels>`. (:issue:`15694`)
370371

371372

372373
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations
@@ -746,12 +747,14 @@ Sorting works as expected
746747
df.sort_index().index.is_lexsorted()
747748
df.sort_index().index.is_monotonic
748749

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

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

756759
Previous Behavior:
757760

pandas/indexes/multi.py

+17
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)