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

Right merge not preserve order #27453

Closed
jesrael opened this issue Jul 18, 2019 · 4 comments · Fixed by #31278
Closed

Right merge not preserve order #27453

jesrael opened this issue Jul 18, 2019 · 4 comments · Fixed by #31278
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@jesrael
Copy link

jesrael commented Jul 18, 2019

Base by SO question:

Merge with right join not preserve order of columns:

columns = ['A', 'B', 'C']
data_1 = [[2, 5, 3, 5], [8, 2, 4, 1], [6, 5, 9, 1]]
data_1 = np.array(data_1).T
df_1 = pd.DataFrame(data=data_1, columns=columns)
print (df_1)
   A  B  C
0  2  8  6
1  5  2  5
2  3  4  9
3  5  1  1

columns = ['A', 'B', 'C']
data_2 = [[2, 5, 3, 5], [7, 1, 3, 0], [np.nan, np.nan, np.nan, np.nan]]
data_2 = np.array(data_2).T
df_2 = pd.DataFrame(data=data_2, columns=columns)
print (df_2)
     A    B   C
0  2.0  7.0 NaN
1  5.0  1.0 NaN
2  3.0  3.0 NaN
3  5.0  0.0 NaN

#right join NOT preserve order
df1 = df_1.merge(df_2[['A', 'B']], on=['A', 'B'], how='right')
print (df1)
   A  B    C
0  5  1  1.0
1  2  7  NaN
2  3  3  NaN
3  5  0  NaN

Correct order for right join:

    A   B   C
0   2   7   NaN
1   5   1   1
2   3   3   NaN
3   5   0   NaN

#left join wpreserve order correct
df2 = df_1.merge(df_2[['A', 'B']], on=['A', 'B'], how='left')
print (df2)
   A  B  C
0  2  8  6
1  5  2  5
2  3  4  9
3  5  1  1

print (pd.show_versions())

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

pandas: 0.24.2
pytest: 4.3.1
pip: 19.0.3
setuptools: 40.8.0
Cython: 0.29.6
numpy: 1.16.2
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 7.4.0
sphinx: 1.8.5
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: 1.2.1
tables: 3.5.1
numexpr: 2.6.9
feather: None
matplotlib: 3.0.3
openpyxl: 2.6.1
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.5
lxml.etree: 4.3.2
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.3.1
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
None
@jschendel jschendel added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jul 18, 2019
@jschendel jschendel added this to the Contributions Welcome milestone Jul 18, 2019
@bongolegend
Copy link
Contributor

bongolegend commented Aug 2, 2019

@jbrockmendel I'm trying to figure out how I might fix this. Am I on the right track by digging into this function?

@jbrockmendel
Copy link
Member

This is a section of the code I don't know particularly well, but I would start by looking at pandas.core.reshape.merge.merge

@bongolegend
Copy link
Contributor

@wesm many years ago you worked on this function, in particular pandas._libs.join.left_outer_join. Are you available to chat about it?

@wesm
Copy link
Member

wesm commented Aug 5, 2019

I'm not sure what I can add to the conversation (beyond acknowledging that I indeed wrote the first version of this function), but if you have specific questions I can try to answer

@jreback jreback modified the milestones: Contributions Welcome, 1.0 Sep 8, 2019
@jreback jreback modified the milestones: 1.0, Contributions Welcome Jan 5, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.1 Feb 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
6 participants