Skip to content

Commit

Permalink
DOC: Add DataFrame.index.levels (#55437)
Browse files Browse the repository at this point in the history
* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   ../pandas/core/indexes/multi.py

* modified:   ../pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* modified:   pandas/core/indexes/multi.py

* Update pandas/core/indexes/multi.py

---------

Co-authored-by: Marc Garcia <[email protected]>
  • Loading branch information
shiersansi and datapythonista authored Oct 12, 2023
1 parent 7e8148f commit 7ef617e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,53 @@ def size(self) -> int:

@cache_readonly
def levels(self) -> FrozenList:
"""
Levels of the MultiIndex.
Levels refer to the different hierarchical levels or layers in a MultiIndex.
In a MultiIndex, each level represents a distinct dimension or category of
the index.
To access the levels, you can use the levels attribute of the MultiIndex,
which returns a tuple of Index objects. Each Index object represents a
level in the MultiIndex and contains the unique values found in that
specific level.
If a MultiIndex is created with levels A, B, C, and the DataFrame using
it filters out all rows of the level C, MultiIndex.levels will still
return A, B, C.
Examples
--------
>>> index = pd.MultiIndex.from_product([['mammal'],
... ('goat', 'human', 'cat', 'dog')], names=['Category', 'Animals'])
>>> leg_num = pd.DataFrame(data=(4, 2, 4, 4), index=index, columns=['Legs'])
>>> leg_num
Legs
Category Animals
mammal goat 4
human 2
cat 4
dog 4
>>> leg_num.index.levels
FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']])
MultiIndex levels will not change even if the DataFrame using the MultiIndex
does not contain all them anymore.
See how "human" is not in the DataFrame, but it is still in levels:
>>> large_leg_num = leg_num[leg_num.Legs > 2]
>>> large_leg_num
Legs
Category Animals
mammal goat 4
cat 4
dog 4
>>> large_leg_num.index.levels
FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']])
"""
# Use cache_readonly to ensure that self.get_locs doesn't repeatedly
# create new IndexEngine
# https://github.com/pandas-dev/pandas/issues/31648
Expand Down

0 comments on commit 7ef617e

Please sign in to comment.