Skip to content
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: additional operator arguments for more explicit control over MultiIndex levels #46962

Open
MMCMA opened this issue May 7, 2022 · 1 comment
Labels
Enhancement MultiIndex Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@MMCMA
Copy link

MMCMA commented May 7, 2022

Is your feature request related to a problem?

More explizit operator (add, sub, mul...) index level control when working with pd.MultiIndex on both index and column dimension

Describe the solution you'd like

Additional arguments in operator function to control both index and column level usage.

API breaking implications

Not sure

Describe alternatives you've considered

See code solution below

Additional context

Maybe there is another more elegant solution already in place and I just missed it

    import pandas as pd

    df_1 = pd.DataFrame([[1, 2], [1, 2, ], [1, 2]])
    df_1.index = pd.MultiIndex.from_arrays([[0, 1, 2], [1, 1, 1]],
                                           names=['idx_0', 'idx_1'])
    df_1.columns = pd.MultiIndex.from_arrays([[1, 2], ['a', 'b']])

    df_2 = pd.DataFrame(dict(a=[2, 3, 4], b=[1, 2, 3]))

    # uses per default idx_1 of index to align multiplication, but I need idx_0
    df_1.mul(df_2, axis=0, level=1)

   # Current solution
    df_1.index = df_1.swaplevel()
    df_1.mul(df_2, axis=0, level=1)
    df_1.index = df_1.swaplevel()
@MMCMA MMCMA added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels May 7, 2022
@MMCMA MMCMA changed the title ENH: ENH: additional operator arguments for more expliziz control over MultiIndex levels May 7, 2022
@MMCMA MMCMA changed the title ENH: additional operator arguments for more expliziz control over MultiIndex levels ENH: additional operator arguments for more explicit control over MultiIndex levels May 7, 2022
@samukweku
Copy link
Contributor

samukweku commented May 7, 2022

I think your current solution is good : df_1.swaplevel().mul(df_2, axis=0, level=1).swaplevel(); I think the issue stems from the fact that the dataframes have different indices, and as such some alignment must occur; if you try : df_1.mul(df_2.set_index(df_1.index), axis = 0, level = 1), it works out fine. It might be an easy stuff to implement - I feel it might introduce another level of complexity (MultiIndex is already a relatively complex idea), and may not be so intuitive long run. So maybe the onus should be on the user to align the indices (either index or columns) before running the binary operation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement MultiIndex Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants