-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Binary operations don't broadcast across multiindex #5645
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
Comments
@roblevy can you put your solution in the top section as well (then its easy to create expected result) |
Done! |
gr8! love for you to work on this! otherwise prob won't be tackled for a while. Its not too tricky and would get you a lot of exposure to the code. |
:D Great On 5 December 2013 23:18, jreback [email protected] wrote:
|
I think the example result has editing errors in it, where did the 400 come from? doesn't match your A very powerful operation to have added. Is there a sugary way to do it you had in mind? |
Hi @y-p. The example result is correct. The 400 is Notice how the values of As far as I'm concerned, this should "just work" when the user does |
Thanks, I see now. |
@keyscores well, this is dependent on #6360 so that needs to be fixed first of course a PR would be welcom |
@keyscores unfortunately no....:) that IS being worked on though |
Ok it was worth a try. At least you know you have a cheerleader for that fix. Would love to see it in .14 :) |
This may or may not be relevant, but I've found a comparatively neat workaround for this problem:
This is not the best possible solution since it changes the order of the index of |
yes this would be quite efficient
|
Looks to me like you might (sometimes at least) need a
|
This appears to have been fixed somewhere, in the latest master you can do import pandas as pd
x = pd.DataFrame({'year':[1,1,1,1,2,2,2,2],
'country':['A','A','B','B','A','A','B','B'],
'prod':[1,2,1,2,1,2,1,2],
'val':[10,20,15,25,20,30,25,35]})
x = x.set_index(['year','country','prod']).squeeze()
y = pd.DataFrame({'year':[1,1,2,2],'prod':[1,2,1,2],
'mul':[10,0.1,20,0.2]})
y = y.set_index(['year','prod']).squeeze()
x.mul(y, axis=0)
# result below
year prod country
1 1 A 100.0
B 150.0
2 A 2.0
B 2.5
2 1 A 400.0
B 500.0
2 A 6.0
B 7.0
dtype: float64 |
Closing as this seems addressed |
related #6360
Based on this SO question
Consider the following two Series:
which look like:
I find it to be an extremely common task, to perform binary operations by distributing the values of
y
over a particular level ofx
. For example, I'd like to multiply all values of product 1 in year 1 by 10.0, regardless ofcountry
.The required result is therefore as follows:
The binary operation .mul() doesn't work as expected:
To create the required result, the user currently has to do this:
The text was updated successfully, but these errors were encountered: