From fbcacff43ad2d19697015161b15b445e2b95dbcf Mon Sep 17 00:00:00 2001 From: Madhuri Patil <60868865+madhuri-15@users.noreply.github.com> Date: Fri, 13 Aug 2021 15:36:56 +0530 Subject: [PATCH 1/5] DOC #42124. Add example to reorder levels --- pandas/core/frame.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 823de2133f0b3..c5ff3a9822fb9 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6806,6 +6806,66 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame: Returns ------- DataFrame + + Example + ------- + >>> df = pd.DataFrame(np.random.randint(10, size=(4, 2)), + ... index=[['a', 'a', 'b', 'b'],[1, 2, 1, 2]], + ... columns=['one', 'two']) + >>> df.index.set_names(['index1', 'index2'], inplace=True) + >>> df + one two + index1 index2 + a 1 7 4 + 2 2 5 + b 1 4 4 + 2 5 2 + + Reorder levels by position + + >>> df.reorder_levels([1, 0]) + one two + index2 index1 + 1 a 7 4 + 2 a 2 5 + 1 b 4 4 + 2 b 5 2 + + Reorder levels by lebels + + >>> df.reorder_levels(['index2', 'index1']) + one two + index2 index1 + 1 a 7 4 + 2 a 2 5 + 1 b 4 4 + 2 b 5 2 + + Length of order must be same as number of levels. + By default, it reorder levels by index, to reorder by columns, use axis = 1. + + >>> df = pd.DataFrame(np.random.randint(10, size=(4, 3)), + ... index=[1, 2, 3, 4], + ... columns=[['A', 'A', 'B'],['one', 'two', 'three']]) + + >>> df.columns.set_names(['column1', 'column2'], inplace=True) + >>> df + column1 A B + column2 one two three + 1 7 3 7 + 2 6 7 6 + 3 2 0 8 + 4 8 4 3 + + + >>> df.reorder_levels([1, 0], axis=1) + column2 one two three + column1 A A B + 1 7 3 7 + 2 6 7 6 + 3 2 0 8 + 4 8 4 3 + """ axis = self._get_axis_number(axis) if not isinstance(self._get_axis(axis), MultiIndex): # pragma: no cover From f89f0be7cd747a947073617a85e7ec6f663d5679 Mon Sep 17 00:00:00 2001 From: Madhuri Patil <60868865+madhuri-15@users.noreply.github.com> Date: Sat, 14 Aug 2021 10:12:58 +0530 Subject: [PATCH 2/5] DOC #42124. Add exmaple to reorder levels --- pandas/core/frame.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c5ff3a9822fb9..28108faa14c6d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6809,9 +6809,9 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame: Example ------- - >>> df = pd.DataFrame(np.random.randint(10, size=(4, 2)), - ... index=[['a', 'a', 'b', 'b'],[1, 2, 1, 2]], - ... columns=['one', 'two']) + >>> df = pd.DataFrame(np.random.randint(10, size=(4, 2)), + ... index=[['a', 'a', 'b', 'b'],[1, 2, 1, 2]], + ... columns=['one', 'two']) >>> df.index.set_names(['index1', 'index2'], inplace=True) >>> df one two @@ -6842,7 +6842,7 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame: 2 b 5 2 Length of order must be same as number of levels. - By default, it reorder levels by index, to reorder by columns, use axis = 1. + By default, it reorder levels by index, to reorder by columns, use axis = 1. >>> df = pd.DataFrame(np.random.randint(10, size=(4, 3)), ... index=[1, 2, 3, 4], From 1bfe1639c4931765203635438f6d69591be8fdd6 Mon Sep 17 00:00:00 2001 From: Madhuri Patil <60868865+madhuri-15@users.noreply.github.com> Date: Sat, 14 Aug 2021 10:58:01 +0530 Subject: [PATCH 3/5] DOC #42124. Add exmaple to reorder levels --- pandas/core/frame.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 28108faa14c6d..e0d943a5958c1 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6810,8 +6810,8 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame: Example ------- >>> df = pd.DataFrame(np.random.randint(10, size=(4, 2)), - ... index=[['a', 'a', 'b', 'b'],[1, 2, 1, 2]], - ... columns=['one', 'two']) + ... index=[['a', 'a', 'b', 'b'],[1, 2, 1, 2]], + ... columns=['one', 'two']) >>> df.index.set_names(['index1', 'index2'], inplace=True) >>> df one two @@ -6845,8 +6845,8 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame: By default, it reorder levels by index, to reorder by columns, use axis = 1. >>> df = pd.DataFrame(np.random.randint(10, size=(4, 3)), - ... index=[1, 2, 3, 4], - ... columns=[['A', 'A', 'B'],['one', 'two', 'three']]) + ... index=[1, 2, 3, 4], + ... columns=[['A', 'A', 'B'],['one', 'two', 'three']]) >>> df.columns.set_names(['column1', 'column2'], inplace=True) >>> df From acee7c87b2fcc3a6d1ab194c15b82d6d313a5f0e Mon Sep 17 00:00:00 2001 From: Madhuri Patil <60868865+madhuri-15@users.noreply.github.com> Date: Sun, 15 Aug 2021 16:36:34 +0530 Subject: [PATCH 4/5] DOC #42124. Add exmaple to reorder levels --- pandas/core/frame.py | 153 +++++++++++++++++++++++++++---------------- 1 file changed, 96 insertions(+), 57 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e0d943a5958c1..39363bd06c9ee 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6807,66 +6807,105 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame: ------- DataFrame - Example - ------- - >>> df = pd.DataFrame(np.random.randint(10, size=(4, 2)), - ... index=[['a', 'a', 'b', 'b'],[1, 2, 1, 2]], - ... columns=['one', 'two']) - >>> df.index.set_names(['index1', 'index2'], inplace=True) - >>> df - one two - index1 index2 - a 1 7 4 - 2 2 5 - b 1 4 4 - 2 5 2 - - Reorder levels by position - - >>> df.reorder_levels([1, 0]) - one two - index2 index1 - 1 a 7 4 - 2 a 2 5 - 1 b 4 4 - 2 b 5 2 - - Reorder levels by lebels - - >>> df.reorder_levels(['index2', 'index1']) - one two - index2 index1 - 1 a 7 4 - 2 a 2 5 - 1 b 4 4 - 2 b 5 2 - - Length of order must be same as number of levels. - By default, it reorder levels by index, to reorder by columns, use axis = 1. - - >>> df = pd.DataFrame(np.random.randint(10, size=(4, 3)), - ... index=[1, 2, 3, 4], - ... columns=[['A', 'A', 'B'],['one', 'two', 'three']]) - - >>> df.columns.set_names(['column1', 'column2'], inplace=True) - >>> df - column1 A B - column2 one two three - 1 7 3 7 - 2 6 7 6 - 3 2 0 8 - 4 8 4 3 +Example +------- +>>> df = pd.DataFrame(np.random.randint(10, size=(4, 2)), +... index=[['a', 'a', 'b', 'b'],[1, 2, 1, 2]], +... columns=['one', 'two']) +>>> df.index.set_names(['index1', 'index2'], inplace=True) +>>> df + one two +index1 index2 +a 1 7 4 + 2 4 1 +b 1 4 1 + 2 3 8 +Reorder levels by position - >>> df.reorder_levels([1, 0], axis=1) - column2 one two three - column1 A A B - 1 7 3 7 - 2 6 7 6 - 3 2 0 8 - 4 8 4 3 +>>> df.reorder_levels([1, 0]) + one two +index2 index1 +1 a 7 4 +2 a 4 1 +1 b 4 1 +2 b 3 8 - """ +The `reorder_levels` function returns unsorted index. +The selection of elements may not works and it will raise `UnsortedIndexError`. + +>>> df.loc[(1, 'a'):(2, 'a')] +UnsortedIndexError: 'Key length (2) was greater than MultiIndex lexsort depth (0)' + +To check if index is sorted or not, you can use `is_lexsorted` method on Index +and `lexsort_depth` to check sort depth. + +>>> df.index.is_lexsorted() +False +>>> df.index.lexsort_depth +0 + +Multiindex object to be index and sliced properly. They need to be sorted, +for that we can use `sort_index` method. + +>>> df = df.sort_index() +>>> df + one two +index2 index1 +1 a 7 4 + b 4 1 +2 a 4 1 + b 3 8 +>>> df.index.is_lexsorted() +True +>>> df.index.lexsort_depth +2 + +Now, selection of elements works as expected. + +>>> df.loc[(1, 'a'):(2, 'a')] + one two +index2 index1 +1 a 7 4 + b 4 1 +2 a 4 1 + + +Reorder levels by lebels + +>>> df.reorder_levels(['index2', 'index1']).sort_index() + one two +index2 index1 +1 a 7 4 + b 4 1 +2 a 4 1 + b 3 8 + +Length of order must be same as number of levels. +By default, it reorder levels by index, to reorder by columns, use axis = 1. + +>>> df = pd.DataFrame(np.random.randint(10, size=(4, 3)), +... index=[1, 2, 3, 4], +... columns=[['A', 'A', 'B'],['one', 'two', 'one']]) +>>> df.columns.set_names(['column1', 'column2'], inplace=True) +>>> df +column1 A B +column2 one two one +1 6 1 8 +2 5 2 3 +3 8 4 1 +4 5 2 9 + +To sort column index, we can use same `sort_index()` method with parameter `axis=1` + +>>> df.reorder_levels([1, 0], axis=1).sort_index(axis=1) +column2 one two +column1 A B A +1 6 8 1 +2 5 3 2 +3 8 1 4 +4 5 9 2 +""" axis = self._get_axis_number(axis) if not isinstance(self._get_axis(axis), MultiIndex): # pragma: no cover raise TypeError("Can only reorder levels on a hierarchical axis.") From 11a0043ea7d3406e9f14b71ae2e0a2454755c70a Mon Sep 17 00:00:00 2001 From: Madhuri Patil <60868865+madhuri-15@users.noreply.github.com> Date: Sun, 15 Aug 2021 16:47:49 +0530 Subject: [PATCH 5/5] DOC #42124. Add exmaple to reorder levels --- pandas/core/frame.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 39363bd06c9ee..6e109f4e7af4d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6870,7 +6870,6 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame: b 4 1 2 a 4 1 - Reorder levels by lebels >>> df.reorder_levels(['index2', 'index1']).sort_index() @@ -6896,6 +6895,14 @@ def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame: 3 8 4 1 4 5 2 9 +>>> df.reorder_levels([1, 0], axis=1) +column2 one two one +column1 A A B +1 6 1 8 +2 5 2 3 +3 8 4 1 +4 5 2 9 + To sort column index, we can use same `sort_index()` method with parameter `axis=1` >>> df.reorder_levels([1, 0], axis=1).sort_index(axis=1)