Skip to content

DOC: Added Documentation for MultiIndex.codes #56265

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

Closed
Closed
Changes from all commits
Commits
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
36 changes: 36 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,42 @@ def levshape(self) -> Shape:

@property
def codes(self) -> FrozenList:
'''
codes in MultiIndex refers to Integer codes for each level of
MultiIndex.

The `codes` attribute represents the integer codes that are used
internally to represent each label in the corresponding level of the
MultiIndex. These codes facilitate efficient storage and retrieval of
hierarchical data.

Each element in the list corresponds to a level in the MultiIndex,
and the arrays within the list contain the integer codes for the
labels at that level.

Returns
-------
pandas.core.indexes.frozen.FrozenList
A frozen list containing integer codes for each level of the MultiIndex.

Example
-------
>>> df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
>>> df.set_index(['A', 'B'], inplace=True)
>>> df.index.codes
[array([0, 1, 2]), array([0, 1, 2])]

>>> levels = [[2, 3, 1], ['Gold', 'Silver', 'Bronze']]
>>> codes = [[2, 0, 1], [0, 1, 2]]
>>> index = pd.MultiIndex(levels=levels, codes=codes)
>>> index
MultiIndex([(1, 'Gold'),
(2, 'Silver'),
(3, 'Bronze')],
)
>>> index.codes
FrozenList([[2, 0, 1], [0, 1, 2]])
'''
return self._codes

def _set_codes(
Expand Down