-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Bug: Joining MultiIndex with NaNs #29252
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
Comments
master produces different output
|
In case someone else has the same problem, the following gives the expected output: print(df1.join(df2, how='outer').loc[df1.index]) This is of course only a workaround, which is noticeably slower than a direct join. |
I just ran into this problem, and I would argue that it is more serious than first meets the eye. The behavior is inconsistent between Say we want to join the following dataframes, and one has a missing value in the import pandas as pd
dfa = pd.DataFrame({
'x': [1.0, float('nan')],
'y': [1.0, 2.0]
})
dfb = pd.DataFrame({
'x': [1.0, 2.0],
'y': [1.0, 2.0],
'z': [10.0, 20.0]
}) Joining on just key = ['x']
ia = pd.Index(dfa['x'])
ib = pd.Index(dfb['x'])
ia.difference(ib).tolist()
# [nan]
ia.intersection(ib).tolist()
# [1.0]
dfa.set_index(key).join(dfb.set_index(key), how='inner', rsuffix='b')
# y yb z
# x
# 1.0 1.0 1.0 10.0
dfa.merge(dfb, left_on=key, right_on=key, suffixes=('', 'b'))
# x y yb z
# 0 1.0 1.0 1.0 10.0 But joining on both key = ['x', 'y']
ia = pd.MultiIndex.from_frame(dfa[key])
ib = pd.MultiIndex.from_frame(dfb[key])
ia.difference(ib).tolist()
# []
ia.intersection(ib)
# [(1.0, 1.0), (nan, 2.0)]
dfa.set_index(key).join(dfb.set_index(key), how='inner')
# z
# x y
# 1.0 1.0 10.0
# NaN 2.0 NaN
dfa.merge(dfb, how='inner')
# x y z
# 0 1.0 1.0 10.0 |
Code Sample, a copy-pastable example if possible
Problem description
The index containing the NaN value of
df2
is falsely joined with the index ofdf1
. Therefore, the result contains both values ofdf2
instead of only one value and a NaN value.Expected Output
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
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
The text was updated successfully, but these errors were encountered: