Skip to content

BUG: Merge inner join on index is working as left join for NaN #45811

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

Closed
3 tasks done
hliatrussellinvestments opened this issue Feb 3, 2022 · 3 comments
Closed
3 tasks done
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@hliatrussellinvestments
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

import pandas as pd

df_1 = pd.DataFrame({'a': [1,2,3,4,5],
                     'b': [1,2,None, 4, None],
                     'c': [1,2,3,4,5]}).set_index(['a', 'b'])

df_2 = pd.DataFrame({'a': [1,2,3,4,5],
                     'b': [1,2,3, 4, 5],
                     'c': [1,2,3,4,5]}).set_index(['a', 'b'])

df_merge = pd.merge(df_1, df_2, left_index=True, right_index=True, how='inner')
print(df_merge)

Issue Description

When merging (inner) 2 data frames on index, if the left index contains NaN, then this row might stay in the result. As in the example above, the third row and last row from the left data frame stay in the merge result.

Expected Behavior

I expect only 3 rows in the result. The third row and last row from the left data frame should not be in the result.

Installed Versions

INSTALLED VERSIONS

commit : bb1f651
python : 3.8.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.4.0
numpy : 1.19.0
pytz : 2021.3
dateutil : 2.8.1
pip : 21.3.1
setuptools : 49.2.1
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : 2021.10.0
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.5.4
sqlalchemy : 1.4.31
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@hliatrussellinvestments hliatrussellinvestments added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 3, 2022
@samukweku
Copy link
Contributor

It is advised in the docs to avoid having nulls in the index, to avoid unexpected results. Link

@phofl
Copy link
Member

phofl commented Feb 4, 2022

This basically comes down to #37222

@mroeschke mroeschke added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 11, 2022
@mroeschke
Copy link
Member

Closing as the core issue is specified in #37222

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

4 participants