Skip to content

BUG: binary operator methods (eg mul) don't handle integer level #9463

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

Suppose I have the following dataframe and series:

In [20]: df = pd.DataFrame(np.ones((3,4)), columns=pd.MultiIndex.from_product([['A', 'B'],['a', 'b']]))

In [21]: s = pd.Series({'a':1, 'b':2})

In [22]: df
Out[22]: 
   A     B   
   a  b  a  b
0  1  1  1  1
1  1  1  1  1
2  1  1  1  1

In [23]: s
Out[23]: 
a    1
b    2
dtype: int64

Trying to multiply this by the columns, and matching on the second level, gives the following error:

In [24]: df.mul(s, axis=1, level=1)
...
  File "C:\Anaconda\lib\site-packages\pandas\core\index.py", line 1786, in _join_multi
    raise ValueError("cannot join with no level specified and no overlapping names")

ValueError: cannot join with no level specified and no overlapping names

In any case, this is a wrong error message, as I did specify a level.
Further, this does work and gives the expected output with a named level instead of a integer level:

In [25]: df2 = df.copy()
    ...: df2.columns.names = ['l0', 'l1']

In [35]: df2.mul(s, axis=1, level='l1') # <-- works

Out[35]: 
l0  A     B   
l1  a  b  a  b
0   1  2  1  2
1   1  2  1  2
2   1  2  1  2

Am I correct that this should also work with integer levels?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions