Skip to content

BUG: join/merge of DataFrame does not keep order of index #40608

Closed
@sdementen

Description

@sdementen
  • I have checked that this issue has not already been reported.

The issue may be related to issue #34133

  • I have confirmed this bug exists on the latest version of pandas.

Code Sample, a copy-pastable example

import pandas

idx_single = pandas.RangeIndex(10)
idx_double = pandas.Index(idx_single.tolist() + idx_single.tolist())

# idx_double
df1 = pandas.DataFrame(index=idx_double)
df2 = pandas.DataFrame(index=idx_single, data=1, columns=["one", "two"])
print(df1.index)
print(df1.join(df2, how="left", sort=False).index)
print(df1.merge(df2, how="left", right_index=True, left_index=True, sort=False).index)

Problem description

The index of the joined (or merged) dataframe is not the same as the left hand dataframe even though sort=False.

Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')
Int64Index([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9], dtype='int64')
Int64Index([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9], dtype='int64')

Expected Output

I would expect the index of the joined/merged dataframe to be the same as the left hand dataframe.

Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')
Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')
Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.9.0.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : French_Belgium.1252

pandas : 1.2.3
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions