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

BUG: Index.join preserving names incorrectly from pandas-2.x #57065

Open
2 of 3 tasks
galipremsagar opened this issue Jan 25, 2024 · 2 comments
Open
2 of 3 tasks

BUG: Index.join preserving names incorrectly from pandas-2.x #57065

galipremsagar opened this issue Jan 25, 2024 · 2 comments
Labels
Bug Index Related to the Index class or subclasses Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@galipremsagar
Copy link

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

In [2]: import pandas as pd
In [3]: pd.__version__
Out[3]: '2.2.0'
In [4]: idx1 = pd.Index([1, 1, 2, 3, 4, 4], name='a')
In [5]: idx2 = pd.Index([2, 5, 3, 4], name='b')
In [6]: idx1.join(idx2)
Out[6]: Index([1, 1, 2, 3, 4, 4], dtype='int64', name='a')
In [7]: idx2.join(idx1)
Out[7]: Index([2, 5, 3, 4, 4], dtype='int64', name='b')

Issue Description

When two index objects have different names, in pandas-1.x versions the end result index would have None as index name. Now in pandas-2.x the result index is always preserving the existing index name it is being called upon.

Expected Behavior

In [2]: import pandas as pd
In [3]: pd.__version__
In [4]: idx1 = pd.Index([1, 1, 2, 3, 4, 4], name='a')
In [5]: idx2 = pd.Index([2, 5, 3, 4], name='b')
In [6]: idx1.join(idx2)
Out[6]: Index([1, 1, 2, 3, 4, 4], dtype='int64')
In [7]: idx2.join(idx1)
Out[7]: Index([2, 5, 3, 4, 4], dtype='int64')

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.10.2.final.0
python-bits : 64
OS : Darwin
OS-release : 23.2.0
Version : Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:18 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 2.2.0
numpy : 1.23.2
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 60.2.0
pip : 21.3.1
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 : 8.5.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@galipremsagar galipremsagar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 25, 2024
@galipremsagar
Copy link
Author

cc: @mroeschke

@VISWESWARAN1998
Copy link
Contributor

When left join is performed and the left index contains all the necessary values required for the new sample space they are just returning the copy of left variable.

In first example:

In [6]: idx1.join(idx2)
Out[6]: Index([1, 1, 2, 3, 4, 4], dtype='int64', name='a')

The newly generated index is the copy of idx1. That is why you see the name "a"

And for:

In [7]: idx2.join(idx1)
Out[7]: Index([2, 5, 3, 4, 4], dtype='int64', name='b')

When index does not contain values of idx2 they have to construct a new index object and they are assigning the name of the variable on which join operation is performed. That is why you see the name "b"

In the join operation I simply assigned name as "None" for these two conditions in the pandas library and I can see your desired results:

Test 1:
image

Test 2:
image

But I believe pandas team has had this to be an expected behavior. As I can see in the code they have intentionally added the name to index in many conditions which cannot be a bug.

I believe the name comes from what index is joined and how the join is performed.

Here it is named "b" because it is a left join

In [7]: idx2.join(idx1)
Out[7]: Index([2, 5, 3, 4, 4], dtype='int64', name='b')

If I change this to right join I get "a"

idx2.join(idx1, how="right")
>>> Index([1, 1, 2, 3, 4, 4], dtype='int64', name='a')

Could be an expected behavior but an experienced team member can review it for you.

Thank you,
Visweswaran N

@lithomas1 lithomas1 added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Index Related to the Index class or subclasses and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Index Related to the Index class or subclasses Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants