Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: MultiIndex.to_hierarchical #21613

Merged
merged 8 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-

.. _whatsnew_0240.prior_deprecations:
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,8 @@ def to_frame(self, index=True):

def to_hierarchical(self, n_repeat, n_shuffle=1):
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_hierarchical is listed elsewhere in this file in a doc-string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - it is mentioned as a part of enumeration of methods of MultiIndex - should it be removed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - updated for all of the latest review comments

.. deprecated:: 0.24.0

Return a MultiIndex reshaped to conform to the
shapes given by n_repeat and n_shuffle.

Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you provide a 1-liner what this method is doing

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):
Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1675,15 +1675,19 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these comments are not needed, move the gh issue number to the top of this method

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]])
tm.assert_index_equal(result, expected)
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]])
Expand All @@ -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'),
Expand Down