Skip to content

Reording a multiindex by only specifiying the order on level 0 #4088

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
jankatins opened this issue Jun 30, 2013 · 5 comments · Fixed by #9019
Closed

Reording a multiindex by only specifiying the order on level 0 #4088

jankatins opened this issue Jun 30, 2013 · 5 comments · Fixed by #9019
Labels
Bug MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@jankatins
Copy link
Contributor

starting PR is here: #6647

By reading the docs on Dataframe.reindex(), I would have guessed (not sure what "broadcast" is...) that this works:

arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = zip(*arrays)
index = MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = DataFrame({"a":list("abcdefgh"), "b":list("abcdefgh")}, index=index)
order = ["baz", "bar", "foo", "qux"]
df.reindex(order, level=0) # still returns the same ordering

It works that way on a normal index (without the level argument):

df2 = DataFrame({"a":list("ABCD")}, index=['bar', 'baz', 'foo', 'qux'])
df2.reindex(order) # new ordering

It also works when I pass a list of tuples:

_new_index = []
for _a in order:
    for _b in df.index.levels[1]:
        _new_index.append((_a, _b))
df.reindex(_new_index) # new ordering
@jtratner
Copy link
Contributor

Are you missing a line where you convert tuples to MultiIndex there?

@cpcloud
Copy link
Member

cpcloud commented Jun 30, 2013

also this

In [5]: df.reindex(order)
Out[5]:
         a    b
b a z  NaN  NaN
    r  NaN  NaN
f o o  NaN  NaN
q u x  NaN  NaN

seems wrong.

@jankatins
Copy link
Contributor Author

@jtratner Sorry, yes, that line went missing. Change it above and now it (not..) worked in a new ipython session

@cpcloud it's df2, not df! (but yes, I also encountered this behavior :-) seems that the the string is taken as an iterable)

@jreback
Copy link
Contributor

jreback commented Mar 15, 2014

@JanSchulz turns out this was not implemented kind of trivial once I figured out what the problem was.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
5 participants