Skip to content

Add to_flat_index method to MultiIndex #22866

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

Merged
merged 16 commits into from
Nov 13, 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
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,7 @@ MultiIndex Components
MultiIndex.set_levels
MultiIndex.set_labels
MultiIndex.to_hierarchical
MultiIndex.to_index
MultiIndex.to_frame
MultiIndex.is_lexsorted
MultiIndex.sortlevel
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Other Enhancements
- :meth:`Index.to_frame` now supports overriding column name(s) (:issue:`22580`).
- New attribute :attr:`__git_version__` will return git commit sha of current build (:issue:`21295`).
- Compatibility with Matplotlib 3.0 (:issue:`22790`).
- :meth:`MultiIndex.to_index` has been added to flatten multiple levels into a single-level :class:`Index` object.

.. _whatsnew_0240.api_breaking:

Expand Down
23 changes: 23 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class MultiIndex(Index):
set_levels
set_labels
to_frame
to_index
is_lexsorted
sortlevel
droplevel
Expand Down Expand Up @@ -1198,6 +1199,28 @@ def to_frame(self, index=True, name=None):
result.index = self
return result

Copy link
Contributor

Choose a reason for hiding this comment

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

how about to_flat ?

Copy link
Contributor

Choose a reason for hiding this comment

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

also i would add the identity method to Index itself

def to_index(self, sep=None):
"""
Convert a MultiIndex to an Index of Tuples containing the level values.

.. versionadded:: 0.24.0

Returns
-------
pd.Index : an Index with the MultiIndex data represented in Tuples.

See also
--------
Index
"""
if sep is not None:
# TODO: Add support for separator to return strs instad of tuples
raise NotImplementedError
else:
idx = Index(self.values, tupleize_cols=False)

return idx

def to_hierarchical(self, n_repeat, n_shuffle=1):
"""
.. deprecated:: 0.24.0
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/multi/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import pandas as pd
import pandas.util.testing as tm
import pytest
from pandas import DataFrame, MultiIndex, date_range
from pandas.compat import range

Expand Down Expand Up @@ -169,3 +170,16 @@ def test_to_series_with_arguments(idx):
assert s.values is not idx.values
assert s.index is not idx
assert s.name != idx.name


def test_to_index(idx):
expected = pd.Index((('foo', 'one'), ('foo', 'two'), ('bar', 'one'),
('baz', 'two'), ('qux', 'one'), ('qux', 'two')),
tupleize_cols=False)
result = idx.to_index()
tm.assert_index_equal(result, expected)


def test_to_index_sep_raises(idx):
with pytest.raises(NotImplementedError):
idx.to_index(sep="")