-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
ENH/BUG: Rename of MultiIndex DataFrames does not work #4160
Comments
what are you trying to accomplish, that doesn't make sense, renaming a 2-level to a single-level You can select out the level however; is this what you are after?
|
Hi everybody, Sorry, the example ist not the best. Actually, I did some trend generation today and after updating to pandas current branch a lot of my code didn't work anymore because that rename function used to work as described above.
|
I favour xs here, bit more explicit:
The fact that replace was working before smells like a bug, as @jreback says, it doesn't really make any sense to rename like that... |
renaming doesn't work for multiindexes period, whether it makes sense or not:
|
Now that is a bug/feature request :) Maybe you ought to be able to replace on each level for a MultiIndex, say using
not sure... |
need to add |
not sure level argument works/makes sense since rename allows you to change both index and columns at the same time:s |
Although perhaps my suggestion doesn't either (if want to replace same things as the level name/number)... |
I think this is still an open issue. It would be great to be able to treat the column labels as tuples and just use rename in the "natural" (at least natural to me) way. For example:
|
@8one6 this is an open issue currently. this still waiting for an API to deal with the multi-level API. I am not sure I like the stringifying idea. But haven't thought too much about this. Maybe an actual example would help |
gives
So now I want to do this:
but that doesn't do what I expect, it just gives back the unmodified frame. To accomplish what I want here, I would do:
which gives the desired result:
Basically, in the multi-index context, I was expecting |
a multi-index renam at the moment does not work at all. The issue is how do you rename only part of a level e.g.
but no way to do this (well it doesn't work), but does make sense
|
Ah, ok. So that last code block in your comment, is that a reasonable thing to hope will work at some point? I.e. is there a reason that would be a bad API for doing fully general multilevel renaming? (I think that's what I had tried to achieve with my dict comprehension in my And the other two lines...
is that the current working proposal? Or already implemented? Or up for debate? |
I think the prior dont work (but prob don't need much). The last is a proposed API. I think their is a pull-requests somewhere which does most of this but wasn't finished IIRC. |
This was closed automatically by github, but that was not the intention (#15931 is not related to this) |
hahah had a reference with the word fix in it! |
Is there any news on this? This bug is a constant annoyance when trying to do complex aggregations. Since the 'agg' method doesn't have a way to assign new names to the aggregate columns the recommended method (AFAIK) is to first aggregate and then rename. But something like
returns a multi-index which I then cannot rename. I actually had to write my own wrapper function for aggregations as a workaround. (Happy to share the code if there's interest.) |
Still open if you're interested in working on it.
…On Wed, Feb 20, 2019 at 5:02 AM mwiebusch78 ***@***.***> wrote:
Is there any news on this? This bug is a constant annoyance when trying to
do complex aggregations. Since the 'agg' method doesn't have a way to
assign new names to the aggregate columns the recommended method (AFAIK) is
to first aggregate and then rename. But something like
df.groupby('A').agg({'B': ['mean', 'median'], 'C': ['mean', 'median']}
returns a multi-index which I then cannot rename. I actually had to write
my own wrapper function for aggregations as a workaround. (Happy to share
the code if there's interest.)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#4160 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABQHIjgSL_9hodVQ7D7O9I4INHvpXtbRks5vPSsrgaJpZM4Ay_pV>
.
|
Just sharing a workaround: renaming of tuples works for flattened indices.
|
Just stumbled over this problem. The delicate part is that the function will always return successfully even if Thanks to normanius for the workaround, if a fix is not that easy, maybe the docstring could be extended with a warning? |
xref #14139 for empty MI
Hi everybody,
in the current version renaming of MultiIndex DataFrames does not work. Lets take the following example:
and the following query:
Now, the following renaming does not work
Thanks in advance
Andy
The text was updated successfully, but these errors were encountered: