Skip to content

Incorrect behaviour when merging dfs using on and left_index or right_index with outer #16229

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
alan-wong opened this issue May 4, 2017 · 1 comment

Comments

@alan-wong
Copy link

alan-wong commented May 4, 2017

data

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                'B': ['B0', 'B1', 'B2', 'B3'],
                'C': ['C0', 'C1', 'C2', 'C3'],
                'D': ['D0', 'D1', 'D2', 'D3']},index=[0, 1, 2, 3])
df2 = pd.DataFrame({'A': ['A0', 'A5', 'A6', 'A7'],
                'B': ['B1', 'B5', 'B6', 'B7'],
                'C': ['A1', 'C5', 'C6', 'C7'],
                'D': ['B1', 'D5', 'D6', 'D7']},index=[4, 5, 6, 7])

With merge using left_index=True with outer:

In [68]:
pd.merge(df1, df2, how='outer', left_index=True, left_on='A', right_on='A')

Out[68]:
    A  B_x  C_x  D_x  B_y  C_y  D_y
4  A0   B0   C0   D0   B1   A1   B1
7  A1   B1   C1   D1  NaN  NaN  NaN
7  A2   B2   C2   D2  NaN  NaN  NaN
7  A3   B3   C3   D3  NaN  NaN  NaN
5  A5  NaN  NaN  NaN   B5   C5   D5
6  A6  NaN  NaN  NaN   B6   C6   D6
7  A7  NaN  NaN  NaN   B7   C7   D7

When we try the opposite:

In [69]:
pd.merge(df1, df2, how='outer', right_index=True, left_on='A', right_on='A')

Out[69]:
    A  A_x  B_x  C_x  D_x  A_y  B_y  C_y  D_y
0  A0   A0   B0   C0   D0  NaN  NaN  NaN  NaN
1  A1   A1   B1   C1   D1  NaN  NaN  NaN  NaN
2  A2   A2   B2   C2   D2  NaN  NaN  NaN  NaN
3  A3   A3   B3   C3   D3  NaN  NaN  NaN  NaN
3   4  NaN  NaN  NaN  NaN   A0   B1   A1   B1
3   5  NaN  NaN  NaN  NaN   A5   B5   C5   D5
3   6  NaN  NaN  NaN  NaN   A6   B6   C6   D6
3   7  NaN  NaN  NaN  NaN   A7   B7   C7   D7

I'd expect the left_index version to contain the index of the left df or is my understanding incorrect here and the result should be the opposite of the right_index version?

Version:

In [71]:

pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.19.2
nose: 1.3.7
pip: 9.0.1
setuptools: 17.1.1
Cython: 0.22.1
numpy: 1.11.0
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 3.2.1
sphinx: 1.3.1
patsy: 0.4.0
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.4.3
openpyxl: None
xlrd: 0.9.4
xlwt: None
xlsxwriter: 0.7.3
lxml: None
bs4: 4.3.2
html5lib: None
httplib2: 0.9.2
apiclient: None
sqlalchemy: 1.0.8
pymysql: None
psycopg2: None
jinja2: 2.7.3
boto: None
pandas_datareader: 0.2.1

There seems to be an inconsistency here and bug with left_index version and possibly also the right_index

@jreback
Copy link
Contributor

jreback commented May 4, 2017

dupe of #16228

@jreback jreback closed this as completed May 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants