Skip to content

BUG: DataFrame.join with categorical index results in unexpected reordering #47812

Closed
@daviskirk

Description

@daviskirk

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
ix = ["a", "b"]
df1 = pd.DataFrame({"c1": ix}, index=pd.CategoricalIndex(ix, categories=ix))
df2 = pd.DataFrame({"c2": reversed(ix)}, index=pd.CategoricalIndex(reversed(ix), categories=reversed(ix)))
result = df1.join(df2)
print(result)
#   c1 c2
# a  a  b
# b  b  a

# Note that using `concat` doesn't have this problem:
result_concat = pd.concat([df1, df2], axis=1)
print(result)
#   c1 c2
# a  a  a
# b  b  b

Issue Description

When joining frames that have equivalent categorical index dtypes but use different orders, a join results in unexpected results.
Most notably the index is joined in the wrong order such that the results become out of sync.

Expected Behavior

I would expect the join to synchonize the categorical index such that the result would be:

  c1 c2
a  a  a
b  b  b

Interestingly, this is the exact result I get when using the "join" functionality of "concat" (see example).

Installed Versions

INSTALLED VERSIONS

commit : e8093ba
python : 3.10.4.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-121-generic
Version : #137-Ubuntu SMP Wed Jun 15 13:33:07 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.3
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 61.2.0
pip : 22.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : 1.3.5
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

Labels

BugCategoricalCategorical Data TypeReshapingConcat, 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