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

WIP: add sort_levels to MultiIndex.from_product #14062

Closed
wants to merge 1 commit into from

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Aug 22, 2016

Currently, from_product always sorts levels in the resulting MultiIndex. This means that the result does not necessarily have lexsorted labels/codes.

This PR adds an option to not sort levels when calling from_product. Using this option yields a few benefits:

  1. It's simpler -- resulting levels on the MultiIndex are exactly those you passed in.
  2. It's marginally faster -- you don't need to sort the levels.
  3. The resulting MultiIndex is always lex-sorted. This is handy if you want to be able to index it effeciently.

The downside is that the result can be a little less intuitive, because levels and labels do not have the same sort order (#14015).

I'm suggesting this option because it would be useful for xarray (to fix pydata/xarray#980) -- otherwise we would simply put it in our own utility function.

I'm not certain that this is the best argument name, or that I've described it in a way that is clear if you don't understand MultiIndex internals.

  • tests added / passed
  • passes git diff upstream/master | flake8 --diff
  • whatsnew entry

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?

@jreback
Copy link
Contributor

jreback commented Nov 16, 2016

@shoyer can you update / close (create an issue to track this then)

@shoyer
Copy link
Member Author

shoyer commented Nov 16, 2016

Closed in favor of #14672.

@shoyer shoyer closed this Nov 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unstacking produces flipped array when using decreasing coordinate values
2 participants