Bug: Joining MultiIndex with NaNs · Issue #29252 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

import numpy as np import pandas as pd

df1 = pd.DataFrame(data={'col1': [1.1, 1.2]}, index=pd.MultiIndex.from_product([['A'], [1.0, 2.0]], names=['id1', 'id2'])) df2 = pd.DataFrame(data={'col2': [2.1, 2.2]}, index=pd.MultiIndex.from_product([['A'], [np.NaN, 2.0]], names=['id1', 'id2'])) print(df1.join(df2))

Problem description

The index containing the NaN value of df2 is falsely joined with the index of df1. Therefore, the result contains both values of df2 instead of only one value and a NaN value.

Expected Output

         col1  col2
id1 id2
A   1.0   1.1   NaN
    2.0   1.2   2.2

The expected result was returned by older pandas versions (successfully tested with 0.18.1 ; 0.20.3 ; 0.21.1 ; 0.22.0).

Actual Output

         col1  col2
id1 id2
A   1.0   1.1   2.1
    2.0   1.2   2.2

This result is returned by newer pandas versions, beginning with version 0.23.0 (tested with 0.23.0 ; 0.23.4 ; 0.24.2 ; 0.25.2).

I suppose this is a bug, or am I missing something?

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 0.25.2
numpy : 1.16.2
pytz : 2018.9
dateutil : 2.8.0
pip : 19.0.3
setuptools : 40.8.0
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 : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None