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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
5 changes: 5 additions & 0 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,11 @@ def test_to_hierarchical(self):
tm.assert_index_equal(result, expected)
Copy link
Contributor

Choose a reason for hiding this comment

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

you need to catch all uses of to_hierarchical (or you get the warnings)

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 - not sure if I understand.
The only use of to_hierarchical was in pandas/core/panel.py- this has been substituted.
This existing test is specifically for to_hierarchical - which can be removed once this method is removed in a future version - should these tests be removed as part of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback I think after reviewing changes in #18258 I probably get what is expected - please ignore the comment above - will update

assert result.names == index.names

# GH21613
# .to_hierarchical will be deprecated
with tm.assert_produces_warning(FutureWarning):
result = index.to_hierarchical(2)
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need the result =

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 - yes had missed it - will update together along with other comments


def test_bounds(self):
self.index._bounds

Expand Down