Skip to content

MultiIndex with ordered Categorical level should (maybe) respect ordered #15087

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
TomAugspurger opened this issue Jan 9, 2017 · 2 comments
Closed
Labels
Categorical Categorical Data Type MultiIndex
Milestone

Comments

@TomAugspurger
Copy link
Contributor

Currently we lexsort. We should maybe follow the Categorical.ordered attribute. This is potentially a can of worms though

In [30]: import pandas as pd
    ...:
    ...: x = pd.Categorical(['apples', 'dairy', 'chicken', 'beef', 'apples', 'dairy', 'chicken'], categories=['apples', 'dairy', 'beef', 'chicken'], ordered=True)
    ...: y = pd.Series([1, 2, 1, 2, 1, 2, 1])
    ...: z = pd.Series([3, 4, 2, 1, 3, 2, 1])
    ...:
    ...: df = pd.DataFrame({'z': z, 'x': x, 'y':y})
    ...: df.set_index(['x', 'y']).sort_index()

Out[30]:
           z
x       y
apples  1  3
        1  3
beef    2  1
chicken 1  2
        1  1
dairy   2  4
        2  2

The index is lex-sorted. If it follows the categorical ordering, it'd be

           z
x       y
apples  1  3
        1  3
dairy   2  4
        2  2
beef    2  1
chicken 1  2
        1  1

cc @ thequackdaddy

@TomAugspurger TomAugspurger added this to the 0.20.0 milestone Jan 9, 2017
@TomAugspurger
Copy link
Contributor Author

From a consistency standpoint, we do follow Categorical.ordered when the index is just a single level, so we should probably follow it for MultiIndex too.

@TomAugspurger
Copy link
Contributor Author

Closing in favor of #15058 (Joris had some additional points there)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type MultiIndex
Projects
None yet
Development

No branches or pull requests

1 participant