From 4bb94187d713469ae2ce4f840bbe7d93e48fef66 Mon Sep 17 00:00:00 2001 From: chethan Date: Fri, 1 Dec 2023 01:37:02 +0530 Subject: [PATCH] Added Documentation for MultiIndex.codes --- pandas/core/indexes/multi.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 86693f241ddb1..2eea0c2dff84c 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -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(