-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
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 f5b7e29
modified: pandas/core/indexes/multi.py
shiersansi 2c8b861
modified: pandas/core/indexes/multi.py
shiersansi 6241262
modified: pandas/core/indexes/multi.py
shiersansi aac99e1
modified: pandas/core/indexes/multi.py
shiersansi a3984e1
modified: pandas/core/indexes/multi.py
shiersansi c11f34a
modified: pandas/core/indexes/multi.py
shiersansi f2bf96a
modified: pandas/core/indexes/multi.py
shiersansi 164df6f
modified: pandas/core/indexes/multi.py
shiersansi 14be467
modified: pandas/core/indexes/multi.py
shiersansi f5085f8
modified: pandas/core/indexes/multi.py
shiersansi 1bca09d
modified: pandas/core/indexes/multi.py
shiersansi ad66dfb
modified: pandas/core/indexes/multi.py
shiersansi 9ea6ebb
modified: pandas/core/indexes/multi.py
shiersansi 329e45c
modified: pandas/core/indexes/multi.py
shiersansi b871123
modified: ../pandas/core/indexes/multi.py
shiersansi ca13b9a
modified: ../pandas/core/indexes/multi.py
shiersansi 4865b2a
modified: pandas/core/indexes/multi.py
shiersansi 6eaa176
modified: pandas/core/indexes/multi.py
shiersansi ed8ec94
modified: pandas/core/indexes/multi.py
shiersansi 39eacd2
modified: pandas/core/indexes/multi.py
shiersansi 19298c4
modified: pandas/core/indexes/multi.py
shiersansi 324b682
modified: pandas/core/indexes/multi.py
shiersansi 1e1c873
modified: pandas/core/indexes/multi.py
shiersansi 1d658e0
modified: pandas/core/indexes/multi.py
shiersansi efe5e0a
modified: pandas/core/indexes/multi.py
shiersansi 596d55c
modified: pandas/core/indexes/multi.py
shiersansi 786abee
Update pandas/core/indexes/multi.py
datapythonista File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not clear to me, sorry. I think something like |
||
>>> 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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels a bit strange to jump into an example case before explaining the concept, but I think it's easier to understand this way.