Skip to content

Multiplying Together Series with MultiIndexes #9368

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
burritothief opened this issue Jan 29, 2015 · 2 comments
Closed

Multiplying Together Series with MultiIndexes #9368

burritothief opened this issue Jan 29, 2015 · 2 comments
Labels
Duplicate Report Duplicate issue or pull request MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode Usage Question

Comments

@burritothief
Copy link

I have a question regarding multiplying two Series that have MultiIndexes. In pandas, I can multiply these two Series and it makes sense:

>>> iterables = [
    ['A', 'B'],
    ['foo', 'bar', 'baz', 'qux'],
    ['one', 'two', 'three']
]
>>> s1 = pd.Series(range(12), index=pd.MultiIndex.from_product(iterables[1:3], 
                               names=['first', 'second']))
>>> s1
first  second
foo    one        0
       two        1
       three      2
bar    one        3
       two        4
       three      5
baz    one        6
       two        7
       three      8
qux    one        9
       two       10
       three     11
dtype: int64
>>> s2  = pd.Series(range(4), index=pd.MultiIndex.from_product([iterables[1]], 
                                        names=['first']))
>>> s2
first
foo      0
bar      1
baz      2
qux      3
dtype: int64
>>> s2 * s1
first  second
foo    one        0
       two        0
       three      0
bar    one        3
       two        4
       three      5
baz    one       12
       two       14
       three     16
qux    one       27
       two       30
       three     33
dtype: int64

I want to do the same thing with deeper MultiIndexes, but it raises an exception:

>>> s1 = pd.Series(range(24),index=pd.MultiIndex.from_product(iterables, 
                               names=['first', 'second', 'third']))
>>> s1
first  second  third
A      foo     one       0
               two       1
               three     2
       bar     one       3
               two       4
               three     5
       baz     one       6
               two       7
               three     8
       qux     one       9
               two      10
               three    11
B      foo     one      12
               two      13
               three    14
       bar     one      15
               two      16
               three    17
       baz     one      18
               two      19
               three    20
       qux     one      21
               two      22
               three    23
dtype: int64
>>> s2 = pd.Series(range(8),index=pd.MultiIndex.from_product(iterables[:2], 
                                names=['first', 'second']))
>>> s2
first  second
A      foo       0
       bar       1
       baz       2
       qux       3
B      foo       4
       bar       5
       baz       6
       qux       7
dtype: int64
>>> s2 * s1 # raises an exception

Shouldn't I be able to multiply these two Series together without raising an exception? Am I doing something wrong?

@shoyer
Copy link
Member

shoyer commented Jan 30, 2015

The error message I get when trying your example is NotImplementedError: merging with more than one level overlap on a multi-index is not implemented. So it's not exactly that you're doing something wrong, but rather that you're doing something that has not yet been implemented... which is exactly what the error says.

@jreback
Copy link
Contributor

jreback commented Jan 30, 2015

here's the original issue
#5645
Here is a pr to implement this, but not finished ATM: #8898

You can do it 'manually' ATM.

In [66]: merged = pd.merge(s1.reset_index(),s2.reset_index(),on=['first','second'])

In [67]: result = merged['0_x']*merged['0_y']

In [68]: result.index = s1.index

In [69]: result
Out[69]: 
first  second  third
A      foo     one        0
               two        0
               three      0
       bar     one        3
               two        4
               three      5
       baz     one       12
               two       14
               three     16
       qux     one       27
               two       30
               three     33
B      foo     one       48
               two       52
               three     56
       bar     one       75
               two       80
               three     85
       baz     one      108
               two      114
               three    120
       qux     one      147
               two      154
               three    161
dtype: int64

@jreback jreback closed this as completed Jan 30, 2015
@jreback jreback added Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jan 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants