Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Add DataFrame.index.levels #55437

Merged
merged 28 commits into from
Oct 12, 2023
Merged
Changes from 18 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
50ff6c2
modified: pandas/core/indexes/multi.py
shiersansi Oct 7, 2023
f5b7e29
modified: pandas/core/indexes/multi.py
shiersansi Oct 7, 2023
2c8b861
modified: pandas/core/indexes/multi.py
shiersansi Oct 7, 2023
6241262
modified: pandas/core/indexes/multi.py
shiersansi Oct 8, 2023
aac99e1
modified: pandas/core/indexes/multi.py
shiersansi Oct 8, 2023
a3984e1
modified: pandas/core/indexes/multi.py
shiersansi Oct 8, 2023
c11f34a
modified: pandas/core/indexes/multi.py
shiersansi Oct 8, 2023
f2bf96a
modified: pandas/core/indexes/multi.py
shiersansi Oct 8, 2023
164df6f
modified: pandas/core/indexes/multi.py
shiersansi Oct 8, 2023
14be467
modified: pandas/core/indexes/multi.py
shiersansi Oct 8, 2023
f5085f8
modified: pandas/core/indexes/multi.py
shiersansi Oct 8, 2023
1bca09d
modified: pandas/core/indexes/multi.py
shiersansi Oct 9, 2023
ad66dfb
modified: pandas/core/indexes/multi.py
shiersansi Oct 9, 2023
9ea6ebb
modified: pandas/core/indexes/multi.py
shiersansi Oct 9, 2023
329e45c
modified: pandas/core/indexes/multi.py
shiersansi Oct 9, 2023
b871123
modified: ../pandas/core/indexes/multi.py
shiersansi Oct 11, 2023
ca13b9a
modified: ../pandas/core/indexes/multi.py
shiersansi Oct 11, 2023
4865b2a
modified: pandas/core/indexes/multi.py
shiersansi Oct 11, 2023
6eaa176
modified: pandas/core/indexes/multi.py
shiersansi Oct 12, 2023
ed8ec94
modified: pandas/core/indexes/multi.py
shiersansi Oct 12, 2023
39eacd2
modified: pandas/core/indexes/multi.py
shiersansi Oct 12, 2023
19298c4
modified: pandas/core/indexes/multi.py
shiersansi Oct 12, 2023
324b682
modified: pandas/core/indexes/multi.py
shiersansi Oct 12, 2023
1e1c873
modified: pandas/core/indexes/multi.py
shiersansi Oct 12, 2023
1d658e0
modified: pandas/core/indexes/multi.py
shiersansi Oct 12, 2023
efe5e0a
modified: pandas/core/indexes/multi.py
shiersansi Oct 12, 2023
596d55c
modified: pandas/core/indexes/multi.py
shiersansi Oct 12, 2023
786abee
Update pandas/core/indexes/multi.py
datapythonista Oct 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,36 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if a MultiIndex is created with levels A, B, C, and the DataFrame using
If a MultiIndex is created with levels A, B, C, and the DataFrame using

Feels a bit strange to jump into an example case before explaining the concept, but I think it's easier to understand this way.

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.index.levels
datapythonista marked this conversation as resolved.
Show resolved Hide resolved
FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']])
datapythonista marked this conversation as resolved.
Show resolved Hide resolved

If the number of Legs is greater than 2 as the filter condition,
the levels do not change
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not clear to me, sorry. I think something like MultiIndex levels will not change even if the DataFrame using the MultiIndex does not contain all them anymore:

>>> large_leg_num = leg_num[leg_num.Legs > 2]
>>> large_leg_num.index.levels
datapythonista marked this conversation as resolved.
Show resolved Hide resolved
FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']])
"""
# Use cache_readonly to ensure that self.get_locs doesn't repeatedly
datapythonista marked this conversation as resolved.
Show resolved Hide resolved
# create new IndexEngine
# https://github.com/pandas-dev/pandas/issues/31648
Expand Down
Loading