Skip to content

API: concat of frames with multi-index with non-matching names #10187

Open
@jorisvandenbossche

Description

@jorisvandenbossche

Suppose you have two dataframes with multi-index with non-matching names. When you concat these, the name of the first index will be used, and the indices are just merged ignoring the names of the levels.

Small example:

In [41]: df1 = pd.DataFrame([[1,2], [3,4]], index=pd.MultiIndex.from_tuples([('a', 1), ('a', 2)], names=['A', 'B']))
In [42]: df2 = pd.DataFrame([[5,6], [7,8]], index=pd.MultiIndex.from_tuples([(1, 'b'), (2, 'b')], names=['B', 'A']))

In [43]: df1
Out[43]:
     0  1
A B
a 1  1  2
  2  3  4

In [44]: df2
Out[44]:
     0  1
B A
1 b  5  6
2 b  7  8

In [45]: pd.concat([df1, df2])
Out[45]:
     0  1
A B
a 1  1  2
  2  3  4
1 b  5  6
2 b  7  8

This of course stems from the fact that index (level) names are not 'real' labels like eg column names, but following the aligning logic in pandas for index labels, this can give a bit unexpected results.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions