Skip to content

Commit b421dcd

Browse files
KalyanGokhalevictor
authored and
victor
committed
DEPR: MultiIndex.to_hierarchical (pandas-dev#21613)
1 parent 72a3f52 commit b421dcd

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Deprecations
110110
~~~~~~~~~~~~
111111

112112
- :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`).
113-
-
113+
- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`21613`)
114114
-
115115

116116
.. _whatsnew_0240.prior_deprecations:

pandas/core/indexes/multi.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ class MultiIndex(Index):
189189
from_product
190190
set_levels
191191
set_labels
192-
to_hierarchical
193192
to_frame
194193
is_lexsorted
195194
sortlevel
@@ -1182,6 +1181,8 @@ def to_frame(self, index=True):
11821181

11831182
def to_hierarchical(self, n_repeat, n_shuffle=1):
11841183
"""
1184+
.. deprecated:: 0.24.0
1185+
11851186
Return a MultiIndex reshaped to conform to the
11861187
shapes given by n_repeat and n_shuffle.
11871188
@@ -1216,6 +1217,9 @@ def to_hierarchical(self, n_repeat, n_shuffle=1):
12161217
# Assumes that each label is divisible by n_shuffle
12171218
labels = [x.reshape(n_shuffle, -1).ravel(order='F') for x in labels]
12181219
names = self.names
1220+
warnings.warn("Method .to_hierarchical is deprecated and will "
1221+
"be removed in a future version",
1222+
FutureWarning, stacklevel=2)
12191223
return MultiIndex(levels=levels, labels=labels, names=names)
12201224

12211225
@property

pandas/core/panel.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -948,10 +948,14 @@ def to_frame(self, filter_observations=True):
948948
data[item] = self[item].values.ravel()[selector]
949949

950950
def construct_multi_parts(idx, n_repeat, n_shuffle=1):
951-
axis_idx = idx.to_hierarchical(n_repeat, n_shuffle)
952-
labels = [x[selector] for x in axis_idx.labels]
953-
levels = axis_idx.levels
954-
names = axis_idx.names
951+
# Replicates and shuffles MultiIndex, returns individual attributes
952+
labels = [np.repeat(x, n_repeat) for x in idx.labels]
953+
# Assumes that each label is divisible by n_shuffle
954+
labels = [x.reshape(n_shuffle, -1).ravel(order='F')
955+
for x in labels]
956+
labels = [x[selector] for x in labels]
957+
levels = idx.levels
958+
names = idx.names
955959
return labels, levels, names
956960

957961
def construct_index_parts(idx, major=True):

pandas/tests/indexes/test_multi.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1673,17 +1673,20 @@ def test_to_frame(self):
16731673
tm.assert_frame_equal(result, expected)
16741674

16751675
def test_to_hierarchical(self):
1676+
# GH21613
16761677
index = MultiIndex.from_tuples([(1, 'one'), (1, 'two'), (2, 'one'), (
16771678
2, 'two')])
1678-
result = index.to_hierarchical(3)
1679+
with tm.assert_produces_warning(FutureWarning):
1680+
result = index.to_hierarchical(3)
16791681
expected = MultiIndex(levels=[[1, 2], ['one', 'two']],
16801682
labels=[[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
16811683
[0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1]])
16821684
tm.assert_index_equal(result, expected)
16831685
assert result.names == index.names
16841686

16851687
# K > 1
1686-
result = index.to_hierarchical(3, 2)
1688+
with tm.assert_produces_warning(FutureWarning):
1689+
result = index.to_hierarchical(3, 2)
16871690
expected = MultiIndex(levels=[[1, 2], ['one', 'two']],
16881691
labels=[[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
16891692
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]])
@@ -1694,8 +1697,8 @@ def test_to_hierarchical(self):
16941697
index = MultiIndex.from_tuples([(2, 'c'), (1, 'b'),
16951698
(2, 'a'), (2, 'b')],
16961699
names=['N1', 'N2'])
1697-
1698-
result = index.to_hierarchical(2)
1700+
with tm.assert_produces_warning(FutureWarning):
1701+
result = index.to_hierarchical(2)
16991702
expected = MultiIndex.from_tuples([(2, 'c'), (2, 'c'), (1, 'b'),
17001703
(1, 'b'),
17011704
(2, 'a'), (2, 'a'),

0 commit comments

Comments
 (0)