From 79d19e4649f6994587024d00a047526f5ccd5439 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 24 Jun 2018 17:06:31 +0530 Subject: [PATCH 1/8] Initial commit MItoHier --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/core/indexes/multi.py | 5 +++++ pandas/core/panel.py | 11 +++++++---- pandas/tests/indexes/test_multi.py | 5 +++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index c23ed006ff637..13e0d80172dc7 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -83,7 +83,7 @@ Deprecations ~~~~~~~~~~~~ - :meth:`DataFrame.to_stata`, :meth:`read_stata`, :class:`StataReader` and :class:`StataWriter` have deprecated the ``encoding`` argument. The encoding of a Stata dta file is determined by the file type and cannot be changed (:issue:`21244`). -- +- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`18262`) - .. _whatsnew_0240.prior_deprecations: diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index ab23a80acdaae..11c6f8a746136 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1182,6 +1182,8 @@ def to_frame(self, index=True): def to_hierarchical(self, n_repeat, n_shuffle=1): """ + DEPRECATED: to_hierarchical will be removed in a future version. + Return a MultiIndex reshaped to conform to the shapes given by n_repeat and n_shuffle. @@ -1216,6 +1218,9 @@ def to_hierarchical(self, n_repeat, n_shuffle=1): # Assumes that each label is divisible by n_shuffle labels = [x.reshape(n_shuffle, -1).ravel(order='F') for x in labels] names = self.names + warnings.warn("Method .to_hierarchical is deprecated and will " + "be removed in a future version", + FutureWarning, stacklevel=2) return MultiIndex(levels=levels, labels=labels, names=names) @property diff --git a/pandas/core/panel.py b/pandas/core/panel.py index c4aa471b8b944..52ed413a55e87 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -948,10 +948,13 @@ def to_frame(self, filter_observations=True): data[item] = self[item].values.ravel()[selector] def construct_multi_parts(idx, n_repeat, n_shuffle=1): - axis_idx = idx.to_hierarchical(n_repeat, n_shuffle) - labels = [x[selector] for x in axis_idx.labels] - levels = axis_idx.levels - names = axis_idx.names + labels = [np.repeat(x, n_repeat) for x in idx.labels] + # Assumes that each label is divisible by n_shuffle + labels = [x.reshape(n_shuffle, -1).ravel(order='F') + for x in labels] + labels = [x[selector] for x in labels] + levels = idx.levels + names = idx.names return labels, levels, names def construct_index_parts(idx, major=True): diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index ab53002ee1587..e22a8104d6c46 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -1704,6 +1704,11 @@ def test_to_hierarchical(self): tm.assert_index_equal(result, expected) assert result.names == index.names + # Xref GH18262 + # .to_hierarchical will be deprecated + with tm.assert_produces_warning(FutureWarning): + result = index.to_hierarchical(2) + def test_bounds(self): self.index._bounds From 007f26f72656812dfa5cd4f73b544d8cb3da8ca8 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 24 Jun 2018 17:45:35 +0530 Subject: [PATCH 2/8] Fixed Depcrecation issue with Sphinx directive MItoHier --- 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 11c6f8a746136..47f62b53741f4 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1182,7 +1182,7 @@ def to_frame(self, index=True): def to_hierarchical(self, n_repeat, n_shuffle=1): """ - DEPRECATED: to_hierarchical will be removed in a future version. + DEPRECATED. to_hierarchical will be removed in a future version. Return a MultiIndex reshaped to conform to the shapes given by n_repeat and n_shuffle. From cd78d937bddce971dffeaf2f3e67c8b99dd607ed Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 24 Jun 2018 18:29:48 +0530 Subject: [PATCH 3/8] Fixed Sphinx deprecation build error MItoHier --- 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 47f62b53741f4..9912306f49ec7 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1182,7 +1182,7 @@ def to_frame(self, index=True): def to_hierarchical(self, n_repeat, n_shuffle=1): """ - DEPRECATED. to_hierarchical will be removed in a future version. + .. deprecated:: 0.24.0 Return a MultiIndex reshaped to conform to the shapes given by n_repeat and n_shuffle. From 8727f95c4bfd6c61dcab7077cf0b8a3c1c4746e2 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Sun, 24 Jun 2018 20:51:39 +0530 Subject: [PATCH 4/8] Updated issue number MItoHier --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/tests/indexes/test_multi.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 13e0d80172dc7..dbe5c481160a3 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -83,7 +83,7 @@ Deprecations ~~~~~~~~~~~~ - :meth:`DataFrame.to_stata`, :meth:`read_stata`, :class:`StataReader` and :class:`StataWriter` have deprecated the ``encoding`` argument. The encoding of a Stata dta file is determined by the file type and cannot be changed (:issue:`21244`). -- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`18262`) +- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`21613`) - .. _whatsnew_0240.prior_deprecations: diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index e22a8104d6c46..7dc183f686672 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -1704,7 +1704,7 @@ def test_to_hierarchical(self): tm.assert_index_equal(result, expected) assert result.names == index.names - # Xref GH18262 + # GH21613 # .to_hierarchical will be deprecated with tm.assert_produces_warning(FutureWarning): result = index.to_hierarchical(2) From 7aa3bbfef5439ad6e15c91da12e649d407a65b60 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Mon, 25 Jun 2018 20:04:22 +0530 Subject: [PATCH 5/8] Suppressed warnings MItoHier --- pandas/tests/indexes/test_multi.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index 7dc183f686672..3d247a60dcb72 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -1675,7 +1675,10 @@ def test_to_frame(self): def test_to_hierarchical(self): index = MultiIndex.from_tuples([(1, 'one'), (1, 'two'), (2, 'one'), ( 2, 'two')]) - result = index.to_hierarchical(3) + # GH21613 + # Suppressed deprecation warnings in this original test + with tm.assert_produces_warning(FutureWarning): + result = index.to_hierarchical(3) expected = MultiIndex(levels=[[1, 2], ['one', 'two']], labels=[[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1]]) @@ -1683,7 +1686,8 @@ def test_to_hierarchical(self): assert result.names == index.names # K > 1 - result = index.to_hierarchical(3, 2) + with tm.assert_produces_warning(FutureWarning): + result = index.to_hierarchical(3, 2) expected = MultiIndex(levels=[[1, 2], ['one', 'two']], labels=[[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]]) @@ -1694,8 +1698,8 @@ def test_to_hierarchical(self): index = MultiIndex.from_tuples([(2, 'c'), (1, 'b'), (2, 'a'), (2, 'b')], names=['N1', 'N2']) - - result = index.to_hierarchical(2) + with tm.assert_produces_warning(FutureWarning): + result = index.to_hierarchical(2) expected = MultiIndex.from_tuples([(2, 'c'), (2, 'c'), (1, 'b'), (1, 'b'), (2, 'a'), (2, 'a'), @@ -1704,11 +1708,6 @@ def test_to_hierarchical(self): tm.assert_index_equal(result, expected) assert result.names == index.names - # GH21613 - # .to_hierarchical will be deprecated - with tm.assert_produces_warning(FutureWarning): - result = index.to_hierarchical(2) - def test_bounds(self): self.index._bounds From 2e5a6da54ae086cd0c4e7d6cc28dfd89ee00dc0e Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Mon, 25 Jun 2018 21:34:49 +0530 Subject: [PATCH 6/8] Force Travis rerun MItoHier --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index dbe5c481160a3..74e664d326b14 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -83,7 +83,7 @@ Deprecations ~~~~~~~~~~~~ - :meth:`DataFrame.to_stata`, :meth:`read_stata`, :class:`StataReader` and :class:`StataWriter` have deprecated the ``encoding`` argument. The encoding of a Stata dta file is determined by the file type and cannot be changed (:issue:`21244`). -- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`21613`) +- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in future version (:issue:`21613`) - .. _whatsnew_0240.prior_deprecations: From 3eedafc95fa94dbb169aaadf2021c54b56e51af2 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Mon, 25 Jun 2018 21:35:08 +0530 Subject: [PATCH 7/8] Force Travis rerun MItoHier --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 74e664d326b14..dbe5c481160a3 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -83,7 +83,7 @@ Deprecations ~~~~~~~~~~~~ - :meth:`DataFrame.to_stata`, :meth:`read_stata`, :class:`StataReader` and :class:`StataWriter` have deprecated the ``encoding`` argument. The encoding of a Stata dta file is determined by the file type and cannot be changed (:issue:`21244`). -- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in future version (:issue:`21613`) +- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`21613`) - .. _whatsnew_0240.prior_deprecations: From 1d2eb264970ca631f0e3baef0556412ff72dd227 Mon Sep 17 00:00:00 2001 From: KalyanGokhale <4734245+KalyanGokhale@users.noreply.github.com> Date: Tue, 26 Jun 2018 07:55:30 +0530 Subject: [PATCH 8/8] Updated for comments MItoHier --- pandas/core/indexes/multi.py | 1 - pandas/core/panel.py | 1 + pandas/tests/indexes/test_multi.py | 3 +-- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 9912306f49ec7..8339e27651082 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -189,7 +189,6 @@ class MultiIndex(Index): from_product set_levels set_labels - to_hierarchical to_frame is_lexsorted sortlevel diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 52ed413a55e87..c8797f14e1cc8 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -948,6 +948,7 @@ def to_frame(self, filter_observations=True): data[item] = self[item].values.ravel()[selector] def construct_multi_parts(idx, n_repeat, n_shuffle=1): + # Replicates and shuffles MultiIndex, returns individual attributes labels = [np.repeat(x, n_repeat) for x in idx.labels] # Assumes that each label is divisible by n_shuffle labels = [x.reshape(n_shuffle, -1).ravel(order='F') diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index 3d247a60dcb72..362f917e74972 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -1673,10 +1673,9 @@ def test_to_frame(self): tm.assert_frame_equal(result, expected) def test_to_hierarchical(self): + # GH21613 index = MultiIndex.from_tuples([(1, 'one'), (1, 'two'), (2, 'one'), ( 2, 'two')]) - # GH21613 - # Suppressed deprecation warnings in this original test with tm.assert_produces_warning(FutureWarning): result = index.to_hierarchical(3) expected = MultiIndex(levels=[[1, 2], ['one', 'two']],