-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Preserve .names in df.set_index(df.index) #6459
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
Conversation
this is not the way to fix this |
Treating it like a Series doesn't fix the issue described above. What should The more correct behavior, in my opinion, would be to return a 4-level MultiIndex. This would require treating the MultiIndex as a DataFrame here or modify |
@jreback This newest commit is what I'm thinking. Let me know if there's a more elegant way to get the columns out of a MultiIndex. |
@jreback, thanks for the suggestions. Most recent commit has those changes. |
looks good. can you add a note to release.rst and v0.14.0.txt both in the API sections, reference this issue and provide a short explanation (prob just a 1-liner - fi you think more is warranted you can do this in v0.14.0.txt with an example - but only if not clear what the change does) |
@jreback Added to notes and rebased. Sorry for the delay - I've been sick the last few days. |
df = pd.util.testing.makeDataFrame() | ||
df.index.name = 'name' | ||
|
||
assert df.set_index(df.index).index.names == ['name'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use self.assertEquals
for these rather than a bare assert
@jreback Added an example to whatsnew. Let me know if you'd like this branch squashed/rebased. |
looks good |
Preserve .names in df.set_index(df.index) Check that df.set_index(df.index) doesn't convert a MultiIndex to an Index Handle general case of df.set_index([df.index,...]) Cleanup Add to release notes Add equality checks Fix issue on 2.6 Add example to whatsnew
Alright, squashed and rebased. |
ENH: Preserve .names in df.set_index(df.index)
thanks! |
closes #6452.
This causes a slight change in behavior in @jseabold's second example. Previously,
df.set_index(df.index)
would convert aMultiIndex
into anIndex
of tuples:This change makes it so the index remains a
MultiIndex
.