Skip to content

pandas concat does not match index names when concatenating two dataframes with a multiindex #40849

Open
@ikfdarba

Description

@ikfdarba

python 3.7
pd.version '1.1.4' and '1.2.3'

running the following code:

df_top=pd.DataFrame([["i1_top","i2_top",1,2]], columns=["index1","index2","value1","value2"])
df_top.set_index(["index1","index2"],inplace=True)
df_bottom=pd.DataFrame([["i1_bottom","i2_bottom",2,1]], columns=["index1","index2","value2","value1"])
df_bottom.set_index(["index2","index1"],inplace=True)
print("df_top")
print(df_top)
print("df_bottom")
print(df_bottom)
print("pd.concat([df_top,df_bottom])")
print(pd.concat([df_top,df_bottom]))

gives the following output:

df_top
               value1  value2
index1 index2                
i1_top i2_top       1       2
df_bottom
                     value2  value1
index2    index1                   
i2_bottom i1_bottom       2       1
pd.concat([df_top,df_bottom])
                     value1  value2
index1    index2                   
i1_top    i2_top          1       2
i2_bottom i1_bottom       1       2

Apparently the concatenation function does not match the index names, and sets the index names of the concatenated data frame in the order of the first dataframe passed to it.

In this example pd.concat (incorrectly) concatenates index1 to index2 and index2 to index1, disregarding the names of the indices...

Is this the expected behaviour? I would expect the index names to be matched in the concatenation, as with the column names..

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions