From 50ff6c210fb17a7163272999a89b1a0eda5dfa48 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sat, 7 Oct 2023 21:42:29 +0800 Subject: [PATCH 01/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 8955329a7afbf..9f73ea02c9f52 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -840,6 +840,37 @@ def size(self) -> int: @cache_readonly def levels(self) -> FrozenList: + """ + Returns a tuple of Index objects representing the levels of the MultiIndex. + + Each level is an Index object containing unique values from that level of the MultiIndex. + + Returns + ------- + levels : tuple of Index + Tuple of Index objects representing the levels of the MultiIndex. + + Notes + ----- + The levels are returned in the order they appear in the MultiIndex. + If the MultiIndex is sliced, this method still returns the original levels of the MultiIndex. + When using this method on a DataFrame index, it may show "extra" values if the DataFrame has been sliced. + This is because the levels are determined based on the original DataFrame index, not the sliced one. + + Examples + -------- + >>> import pandas as pd + >>> import numpy as np + >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], list('abcde')], names=['Person', 'Letter']) + >>> large = pd.DataFrame(data=np.random.randn(15, 2), index=idx, columns=['one', 'two']) + >>> small = large.loc[['Jo'==d[0:2] for d in large.index.get_level_values('Person')]] + + >>> print(large.index.levels) + FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) + + >>> print(large.index.levels) + FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) + """ # Use cache_readonly to ensure that self.get_locs doesn't repeatedly # create new IndexEngine # https://github.com/pandas-dev/pandas/issues/31648 From f5b7e29ccb6123ea7b433c2237d158507a56961d Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sat, 7 Oct 2023 22:21:31 +0800 Subject: [PATCH 02/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 9f73ea02c9f52..5046eaecc8830 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -841,29 +841,38 @@ def size(self) -> int: @cache_readonly def levels(self) -> FrozenList: """ - Returns a tuple of Index objects representing the levels of the MultiIndex. + Returns a tuple of Index objects representing the levels + of the MultiIndex. - Each level is an Index object containing unique values from that level of the MultiIndex. + Each level is an Index object containing unique values + from that level of the MultiIndex. Returns ------- levels : tuple of Index - Tuple of Index objects representing the levels of the MultiIndex. + Tuple of Index objects representing the levels + of the MultiIndex. Notes ----- The levels are returned in the order they appear in the MultiIndex. - If the MultiIndex is sliced, this method still returns the original levels of the MultiIndex. - When using this method on a DataFrame index, it may show "extra" values if the DataFrame has been sliced. - This is because the levels are determined based on the original DataFrame index, not the sliced one. + If the MultiIndex is sliced, this method still returns the original + levels of the MultiIndex. + When using this method on a DataFrame index, it may show "extra" + values if the DataFrame has been sliced. + This is because the levels are determined based on the original + DataFrame index, not the sliced one. Examples -------- >>> import pandas as pd >>> import numpy as np - >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], list('abcde')], names=['Person', 'Letter']) - >>> large = pd.DataFrame(data=np.random.randn(15, 2), index=idx, columns=['one', 'two']) - >>> small = large.loc[['Jo'==d[0:2] for d in large.index.get_level_values('Person')]] + >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], + ...list('abcde')], names=['Person', 'Letter']) + >>> large = pd.DataFrame(data=np.random.randn(15, 2), + ...index=idx, columns=['one', 'two']) + >>> small = large.loc[['Jo'==d[0:2] for d in + ...large.index.get_level_values('Person')]] >>> print(large.index.levels) FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) @@ -871,9 +880,6 @@ def levels(self) -> FrozenList: >>> print(large.index.levels) FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) """ - # Use cache_readonly to ensure that self.get_locs doesn't repeatedly - # create new IndexEngine - # https://github.com/pandas-dev/pandas/issues/31648 result = [x._rename(name=name) for x, name in zip(self._levels, self._names)] for level in result: # disallow midx.levels[0].name = "foo" From 2c8b861fc739139fdbc8da83bb544ba7fb04bd84 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sat, 7 Oct 2023 23:14:57 +0800 Subject: [PATCH 03/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 5046eaecc8830..8f0882fa79c06 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -865,8 +865,6 @@ def levels(self) -> FrozenList: Examples -------- - >>> import pandas as pd - >>> import numpy as np >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], ...list('abcde')], names=['Person', 'Letter']) >>> large = pd.DataFrame(data=np.random.randn(15, 2), From 62412624fb3ae856e235fc3cdc0b004d15564006 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sun, 8 Oct 2023 11:59:36 +0800 Subject: [PATCH 04/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 44 +++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 8f0882fa79c06..d49a451f6a06e 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -840,22 +840,34 @@ def size(self) -> int: @cache_readonly def levels(self) -> FrozenList: + # Use cache_readonly to ensure that self.get_locs doesn't repeatedly + # create new IndexEngine + # https://github.com/pandas-dev/pandas/issues/31648 """ - Returns a tuple of Index objects representing the levels - of the MultiIndex. + Levels of the MultiIndex. - Each level is an Index object containing unique values - from that level of the MultiIndex. + What are levels? + ------- + 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. - Returns + How can levels be seen in pandas? ------- - levels : tuple of Index - Tuple of Index objects representing the levels - of the MultiIndex. + 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. + + Examples + -------- + >>> idx = pd.MultiIndex.from_product([(0, 1, 2), ('green', 'purple')], + ... names=['number', 'color']) + >>> idx.levels + [[0, 1, 2], ['green', 'purple']] Notes ----- - The levels are returned in the order they appear in the MultiIndex. If the MultiIndex is sliced, this method still returns the original levels of the MultiIndex. When using this method on a DataFrame index, it may show "extra" @@ -866,17 +878,17 @@ def levels(self) -> FrozenList: Examples -------- >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], - ...list('abcde')], names=['Person', 'Letter']) + ... list('abcde')], names=['Person', 'Letter']) >>> large = pd.DataFrame(data=np.random.randn(15, 2), - ...index=idx, columns=['one', 'two']) + ... index=idx, columns=['one', 'two']) >>> small = large.loc[['Jo'==d[0:2] for d in - ...large.index.get_level_values('Person')]] + ... large.index.get_level_values('Person')]] - >>> print(large.index.levels) - FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) + >>> large.index.levels + [['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']] - >>> print(large.index.levels) - FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) + >>> large.index.levels + [['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']] """ result = [x._rename(name=name) for x, name in zip(self._levels, self._names)] for level in result: From aac99e165600c9fe6f422ab3da006f6af9af255c Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sun, 8 Oct 2023 12:17:10 +0800 Subject: [PATCH 05/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index d49a451f6a06e..c7c0e4034dfff 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -866,29 +866,7 @@ def levels(self) -> FrozenList: >>> idx.levels [[0, 1, 2], ['green', 'purple']] - Notes - ----- - If the MultiIndex is sliced, this method still returns the original - levels of the MultiIndex. - When using this method on a DataFrame index, it may show "extra" - values if the DataFrame has been sliced. - This is because the levels are determined based on the original - DataFrame index, not the sliced one. - Examples - -------- - >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], - ... list('abcde')], names=['Person', 'Letter']) - >>> large = pd.DataFrame(data=np.random.randn(15, 2), - ... index=idx, columns=['one', 'two']) - >>> small = large.loc[['Jo'==d[0:2] for d in - ... large.index.get_level_values('Person')]] - - >>> large.index.levels - [['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']] - - >>> large.index.levels - [['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']] """ result = [x._rename(name=name) for x, name in zip(self._levels, self._names)] for level in result: From a3984e1c121dd44e7f5ccd104e10bd2d15d61a46 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sun, 8 Oct 2023 13:32:38 +0800 Subject: [PATCH 06/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index c7c0e4034dfff..da3842aafebdd 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -847,13 +847,13 @@ def levels(self) -> FrozenList: Levels of the MultiIndex. What are levels? - ------- + ---------------- 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. How can levels be seen in pandas? - ------- + --------------------------------- 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 @@ -866,6 +866,25 @@ def levels(self) -> FrozenList: >>> idx.levels [[0, 1, 2], ['green', 'purple']] + Notes + ----- + If the MultiIndex is sliced, this method still returns the original + levels of the MultiIndex. + When using this method on a DataFrame index, it may show "extra" + + Examples + -------- + >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], + ... list('abcde')], names=['Person', 'Letter']) + >>> large = pd.DataFrame(data=np.random.randn(15, 2), + ... index=idx, columns=['one', 'two']) + >>> small = large.loc[['Jo'==d[0:2] for d in + ... large.index.get_level_values('Person')]] + >>> large.index.levels + [['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']] + >>> small.index.levels + [['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']] + """ result = [x._rename(name=name) for x, name in zip(self._levels, self._names)] From c11f34a187ef9eed1809a393ba500087ec07d213 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sun, 8 Oct 2023 13:58:14 +0800 Subject: [PATCH 07/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index da3842aafebdd..79d84e97f7116 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -847,25 +847,16 @@ def levels(self) -> FrozenList: Levels of the MultiIndex. What are levels? - ---------------- 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. How can levels be seen in pandas? - --------------------------------- 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. - Examples - -------- - >>> idx = pd.MultiIndex.from_product([(0, 1, 2), ('green', 'purple')], - ... names=['number', 'color']) - >>> idx.levels - [[0, 1, 2], ['green', 'purple']] - Notes ----- If the MultiIndex is sliced, this method still returns the original @@ -874,6 +865,13 @@ def levels(self) -> FrozenList: Examples -------- + Example 1: + >>> idx = pd.MultiIndex.from_product([(0, 1, 2), ('green', 'purple')], + ... names=['number', 'color']) + >>> idx.levels + [[0, 1, 2], ['green', 'purple']] + + Example 2: >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], ... list('abcde')], names=['Person', 'Letter']) >>> large = pd.DataFrame(data=np.random.randn(15, 2), From f2bf96ad1e2675958103fd9ab75974e2a8fa0efa Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sun, 8 Oct 2023 14:22:05 +0800 Subject: [PATCH 08/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 79d84e97f7116..ae28a7c89325f 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -869,7 +869,7 @@ def levels(self) -> FrozenList: >>> idx = pd.MultiIndex.from_product([(0, 1, 2), ('green', 'purple')], ... names=['number', 'color']) >>> idx.levels - [[0, 1, 2], ['green', 'purple']] + FrozenList([[0, 1, 2], ['green', 'purple']]) Example 2: >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], @@ -879,9 +879,9 @@ def levels(self) -> FrozenList: >>> small = large.loc[['Jo'==d[0:2] for d in ... large.index.get_level_values('Person')]] >>> large.index.levels - [['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']] + FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) >>> small.index.levels - [['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']] + FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) """ From 164df6fda7364494941ef5b07d6cf97bc37d220b Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sun, 8 Oct 2023 14:33:00 +0800 Subject: [PATCH 09/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index ae28a7c89325f..b01c02bb0ad0d 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -840,9 +840,6 @@ def size(self) -> int: @cache_readonly def levels(self) -> FrozenList: - # Use cache_readonly to ensure that self.get_locs doesn't repeatedly - # create new IndexEngine - # https://github.com/pandas-dev/pandas/issues/31648 """ Levels of the MultiIndex. @@ -861,7 +858,7 @@ def levels(self) -> FrozenList: ----- If the MultiIndex is sliced, this method still returns the original levels of the MultiIndex. - When using this method on a DataFrame index, it may show "extra" + When using this method on a DataFrame index, it may show "extra". Examples -------- @@ -882,9 +879,10 @@ def levels(self) -> FrozenList: FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) >>> small.index.levels FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) - - """ + # Use cache_readonly to ensure that self.get_locs doesn't repeatedly + # create new IndexEngine + # https://github.com/pandas-dev/pandas/issues/31648 result = [x._rename(name=name) for x, name in zip(self._levels, self._names)] for level in result: # disallow midx.levels[0].name = "foo" From 14be46783e79cb659e789d68b2fcc888de59d66d Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sun, 8 Oct 2023 16:53:27 +0800 Subject: [PATCH 10/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index b01c02bb0ad0d..b731b93eb358d 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -843,42 +843,34 @@ def levels(self) -> FrozenList: """ Levels of the MultiIndex. - What are levels? 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. - How can levels be seen in pandas? 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. - Notes - ----- - If the MultiIndex is sliced, this method still returns the original + If the DataFrame not using all levels, this method still returns the original levels of the MultiIndex. When using this method on a DataFrame index, it may show "extra". Examples -------- - Example 1: - >>> idx = pd.MultiIndex.from_product([(0, 1, 2), ('green', 'purple')], - ... names=['number', 'color']) + >>> idx = pd.MultiIndex.from_product([('insect', 'mammal'), + ... ('goat', 'dog','cat','ant','spides')], names=['class', 'animal']) >>> idx.levels - FrozenList([[0, 1, 2], ['green', 'purple']]) + FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) - Example 2: - >>> idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], - ... list('abcde')], names=['Person', 'Letter']) - >>> large = pd.DataFrame(data=np.random.randn(15, 2), + >>> num = pd.DataFrame(data=np.random.randn(10, 2), ... index=idx, columns=['one', 'two']) - >>> small = large.loc[['Jo'==d[0:2] for d in - ... large.index.get_level_values('Person')]] - >>> large.index.levels - FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) - >>> small.index.levels - FrozenList([['Alex', 'John', 'Josh'], ['a', 'b', 'c', 'd', 'e']]) + >>> insect_num = num.loc[['in'==d[0:2] for d in + ... num.index.get_level_values('class')]] + >>> num.index.levels + FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) + >>> insect_num.index.levels + FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) """ # Use cache_readonly to ensure that self.get_locs doesn't repeatedly # create new IndexEngine From f5085f8bb9027201a5800011b90f6e1f466fdcd6 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Sun, 8 Oct 2023 21:20:34 +0800 Subject: [PATCH 11/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index b731b93eb358d..53b47355df9a9 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -863,13 +863,13 @@ def levels(self) -> FrozenList: >>> idx.levels FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) - >>> num = pd.DataFrame(data=np.random.randn(10, 2), - ... index=idx, columns=['one', 'two']) - >>> insect_num = num.loc[['in'==d[0:2] for d in - ... num.index.get_level_values('class')]] - >>> num.index.levels + >>> leg_num = pd.DataFrame(data=(1,2,3,4,5,6,7,8,9,2), + ... index=idx, columns=['leg']) + >>> insect_leg_num = leg_num.loc[['insect'==d for d in + ... leg_num.index.get_level_values('class')]] + >>> leg_num.index.levels FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) - >>> insect_num.index.levels + >>> insect_leg_num.index.levels FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) """ # Use cache_readonly to ensure that self.get_locs doesn't repeatedly From 1bca09dc79cff458d889f0ff040c8bc29db62331 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Mon, 9 Oct 2023 11:39:40 +0800 Subject: [PATCH 12/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 53b47355df9a9..b81713997c549 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -860,16 +860,17 @@ def levels(self) -> FrozenList: -------- >>> idx = pd.MultiIndex.from_product([('insect', 'mammal'), ... ('goat', 'dog','cat','ant','spides')], names=['class', 'animal']) - >>> idx.levels + >>> print(idx.levels) FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) >>> leg_num = pd.DataFrame(data=(1,2,3,4,5,6,7,8,9,2), ... index=idx, columns=['leg']) >>> insect_leg_num = leg_num.loc[['insect'==d for d in ... leg_num.index.get_level_values('class')]] - >>> leg_num.index.levels + + >>> print(leg_num.index.levels) FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) - >>> insect_leg_num.index.levels + >>> print(insect_leg_num.index.levels) FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) """ # Use cache_readonly to ensure that self.get_locs doesn't repeatedly From ad66dfb62c57fb89712c7873858ea5065277433f Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Mon, 9 Oct 2023 12:11:41 +0800 Subject: [PATCH 13/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index b81713997c549..3a5f72a5b3810 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -861,7 +861,7 @@ def levels(self) -> FrozenList: >>> idx = pd.MultiIndex.from_product([('insect', 'mammal'), ... ('goat', 'dog','cat','ant','spides')], names=['class', 'animal']) >>> print(idx.levels) - FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) + [['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']] >>> leg_num = pd.DataFrame(data=(1,2,3,4,5,6,7,8,9,2), ... index=idx, columns=['leg']) @@ -869,9 +869,9 @@ def levels(self) -> FrozenList: ... leg_num.index.get_level_values('class')]] >>> print(leg_num.index.levels) - FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) + [['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']] >>> print(insect_leg_num.index.levels) - FrozenList([['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']]) + [['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']] """ # Use cache_readonly to ensure that self.get_locs doesn't repeatedly # create new IndexEngine From 9ea6ebb1c048619ee628219cc68f7821463459f7 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Mon, 9 Oct 2023 20:40:47 +0800 Subject: [PATCH 14/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 3a5f72a5b3810..05063c1eb736d 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -852,26 +852,24 @@ def levels(self) -> FrozenList: level in the MultiIndex and contains the unique values found in that specific level. - If the DataFrame not using all levels, this method still returns the original - levels of the MultiIndex. - When using this method on a DataFrame index, it may show "extra". + 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 -------- - >>> idx = pd.MultiIndex.from_product([('insect', 'mammal'), - ... ('goat', 'dog','cat','ant','spides')], names=['class', 'animal']) - >>> print(idx.levels) - [['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']] - - >>> leg_num = pd.DataFrame(data=(1,2,3,4,5,6,7,8,9,2), - ... index=idx, columns=['leg']) - >>> insect_leg_num = leg_num.loc[['insect'==d for d in - ... leg_num.index.get_level_values('class')]] - - >>> print(leg_num.index.levels) - [['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']] - >>> print(insect_leg_num.index.levels) - [['insect', 'mammal'], ['ant', 'cat', 'dog', 'goat', 'spides']] + >>> level1 = ['mammal'] + >>> level2 = ['goat', 'human', 'cat', 'dog'] + >>> index = pd.MultiIndex.from_product([level1, level2], + ... names=['Category', 'Animals']) + >>> leg_num = pd.DataFrame(data=(4, 2, 4, 4), index=index, columns=['Legs']) + >>> leg_num.index.levels + FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) + + # If the number of Legs is greater than 2 as the filter condition, the levels do not change + >>> large_leg_num = leg_num[leg_num.Legs > 2] + >>> 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 From 329e45c9d2d74b94c0bce5842982d8595b0ef86b Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Mon, 9 Oct 2023 21:06:19 +0800 Subject: [PATCH 15/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 05063c1eb736d..a63a825a5de10 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -866,7 +866,8 @@ def levels(self) -> FrozenList: >>> leg_num.index.levels FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) - # If the number of Legs is greater than 2 as the filter condition, the levels do not change + # If the number of Legs is greater than 2 as the filter condition, + # the levels do not change >>> large_leg_num = leg_num[leg_num.Legs > 2] >>> large_leg_num.index.levels FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) From b8711239833bccdfac01683fdb5f7f4b4d58e8d8 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Wed, 11 Oct 2023 18:11:08 +0800 Subject: [PATCH 16/28] modified: ../pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index a63a825a5de10..df947aacb0f60 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -858,16 +858,14 @@ def levels(self) -> FrozenList: Examples -------- - >>> level1 = ['mammal'] - >>> level2 = ['goat', 'human', 'cat', 'dog'] - >>> index = pd.MultiIndex.from_product([level1, level2], - ... names=['Category', 'Animals']) + >>> 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 FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) - # If the number of Legs is greater than 2 as the filter condition, - # the levels do not change + If the number of Legs is greater than 2 as the filter condition, + the levels do not change >>> large_leg_num = leg_num[leg_num.Legs > 2] >>> large_leg_num.index.levels FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) From ca13b9a590e603c265ce63ba7b56c0dbf7dd4ff0 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Wed, 11 Oct 2023 18:32:21 +0800 Subject: [PATCH 17/28] modified: ../pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index df947aacb0f60..c187e35e60515 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -858,8 +858,8 @@ def levels(self) -> FrozenList: Examples -------- - >>> index = pd.MultiIndex.from_product([('mammal'), ('goat', 'human', 'cat', 'dog')], - ... names=['Category', 'Animals']) + >>> 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 FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) From 4865b2a5e3164d36cb062071de96714d20496d5e Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Wed, 11 Oct 2023 19:23:35 +0800 Subject: [PATCH 18/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index c187e35e60515..6dceecc00ab63 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -858,8 +858,8 @@ def levels(self) -> FrozenList: Examples -------- - >>> index = pd.MultiIndex.from_product([('mammal'), - ... ('goat', 'human', 'cat', 'dog')],names=['Category', 'Animals']) + >>> 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 FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) From 6eaa176d0ab66a710530035471b09e3ada81a867 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Thu, 12 Oct 2023 09:18:21 +0800 Subject: [PATCH 19/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 6dceecc00ab63..e3f390b4d9eab 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -852,7 +852,7 @@ def levels(self) -> FrozenList: 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 + 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. @@ -861,12 +861,25 @@ def levels(self) -> FrozenList: >>> 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']]) - If the number of Legs is greater than 2 as the filter condition, - the levels do not change + 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 + Legs + Category Animals + mammal goat 4 + cat 4 + dog 4 >>> large_leg_num.index.levels FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) """ From ed8ec9451349f3d4cf9be12b00fe8dd98ad0f11d Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Thu, 12 Oct 2023 11:18:33 +0800 Subject: [PATCH 20/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index e3f390b4d9eab..4de4acc7970dc 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -873,6 +873,7 @@ def levels(self) -> FrozenList: 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 Legs From 39eacd2693927e559eafacee06a54819d3226b7d Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Thu, 12 Oct 2023 11:24:10 +0800 Subject: [PATCH 21/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 4de4acc7970dc..34984ab70dbe6 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -868,12 +868,12 @@ def levels(self) -> FrozenList: 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: - >>> large_leg_num = leg_num[leg_num.Legs > 2] >>> large_leg_num Legs @@ -881,6 +881,7 @@ def levels(self) -> FrozenList: mammal goat 4 cat 4 dog 4 + >>> large_leg_num.index.levels FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) """ From 19298c4428a5e85b5d3ba1657e0a9c824c596d3d Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Thu, 12 Oct 2023 13:07:18 +0800 Subject: [PATCH 22/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 34984ab70dbe6..84bb538c06647 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -874,6 +874,7 @@ def levels(self) -> FrozenList: 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 Legs From 324b68252523b1caa309b95087b8abc95d4469ce Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Thu, 12 Oct 2023 13:16:40 +0800 Subject: [PATCH 23/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 84bb538c06647..81d564749a32d 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -862,27 +862,13 @@ def levels(self) -> FrozenList: ... ('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: - >>> 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']]) """ From 1e1c8733455d7957a972a2c7444b0207dd2976e8 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Thu, 12 Oct 2023 13:31:50 +0800 Subject: [PATCH 24/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 81d564749a32d..b959fbe51fbfb 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -869,6 +869,7 @@ def levels(self) -> FrozenList: does not contain all them anymore: >>> large_leg_num = leg_num[leg_num.Legs > 2] >>> large_leg_num + f >>> large_leg_num.index.levels FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) """ From 1d658e0339ef6c0333030be1a677119976d79ef6 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Thu, 12 Oct 2023 14:13:20 +0800 Subject: [PATCH 25/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index b959fbe51fbfb..e3f390b4d9eab 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -862,6 +862,12 @@ def levels(self) -> FrozenList: ... ('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']]) @@ -869,7 +875,11 @@ def levels(self) -> FrozenList: does not contain all them anymore: >>> large_leg_num = leg_num[leg_num.Legs > 2] >>> large_leg_num - f + Legs + Category Animals + mammal goat 4 + cat 4 + dog 4 >>> large_leg_num.index.levels FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) """ From efe5e0a3dd0861549ae96a95b072b89654bd2117 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Thu, 12 Oct 2023 15:29:52 +0800 Subject: [PATCH 26/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index e3f390b4d9eab..34984ab70dbe6 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -868,6 +868,7 @@ def levels(self) -> FrozenList: human 2 cat 4 dog 4 + >>> leg_num.index.levels FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) @@ -880,6 +881,7 @@ def levels(self) -> FrozenList: mammal goat 4 cat 4 dog 4 + >>> large_leg_num.index.levels FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) """ From 596d55ca14a30e9667851272be487739fba5aa81 Mon Sep 17 00:00:00 2001 From: shier <3207840495@qq.com> Date: Thu, 12 Oct 2023 16:17:48 +0800 Subject: [PATCH 27/28] modified: pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 34984ab70dbe6..c2d2b14a8b782 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -873,7 +873,9 @@ def levels(self) -> FrozenList: FrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']]) MultiIndex levels will not change even if the DataFrame using the MultiIndex - does not contain all them anymore: + 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 From 786abee99c9c6b3a2ac4b45ff222f07b95339c7b Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Thu, 12 Oct 2023 11:37:06 +0200 Subject: [PATCH 28/28] Update pandas/core/indexes/multi.py --- pandas/core/indexes/multi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index c2d2b14a8b782..6ac289ff8e705 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -874,7 +874,7 @@ def levels(self) -> FrozenList: 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: + 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