Skip to content

WIP: add sort_levels to MultiIndex.from_product #14062

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

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
22 changes: 16 additions & 6 deletions pandas/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,8 @@ def from_tuples(cls, tuples, sortorder=None, names=None):
return MultiIndex.from_arrays(arrays, sortorder=sortorder, names=names)

@classmethod
def from_product(cls, iterables, sortorder=None, names=None):
def from_product(cls, iterables, sortorder=None, sort_levels=True,
names=None):
"""
Make a MultiIndex from the cartesian product of multiple iterables

Expand All @@ -930,6 +931,10 @@ def from_product(cls, iterables, sortorder=None, names=None):
sortorder : int or None
Level of sortedness (must be lexicographically sorted by that
level).
sort_levels : boolean
Whether or not to sort internal levels on the resulting index. The
order of values in the result is the same either way, but disabling
this option ensures that it's fully lex-sorted.
names : list / sequence of strings or None
Names for the levels in the index.

Expand All @@ -955,12 +960,17 @@ def from_product(cls, iterables, sortorder=None, names=None):
from pandas.core.categorical import Categorical
from pandas.tools.util import cartesian_product

categoricals = [Categorical.from_array(it, ordered=True)
for it in iterables]
labels = cartesian_product([c.codes for c in categoricals])
if sort_levels:
categoricals = [Categorical.from_array(it, ordered=True)
for it in iterables]
levels = [c.categories for c in categoricals]
labels = cartesian_product([c.codes for c in categoricals])
else:
sortorder = 0
Copy link
Member Author

Choose a reason for hiding this comment

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

possibly should check to make sure we aren't overwriting it?

levels = iterables
labels = cartesian_product([np.arange(len(it)) for it in iterables])

return MultiIndex(levels=[c.categories for c in categoricals],
labels=labels, sortorder=sortorder, names=names)
return MultiIndex(levels, labels, sortorder=sortorder, names=names)

@property
def nlevels(self):
Expand Down