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

merge does not preserve order of data frame when left key has duplicate values #7331

Closed
xdliao opened this issue Jun 4, 2014 · 1 comment · Fixed by #8948
Closed

merge does not preserve order of data frame when left key has duplicate values #7331

xdliao opened this issue Jun 4, 2014 · 1 comment · Fixed by #8948
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@xdliao
Copy link

xdliao commented Jun 4, 2014

import pandas as pd
dates = [  20140101, 20140102, 20140103]
states = [  "CA",  "NY", "CA"]
x = pd.DataFrame({ 'dates' : dates, 'states' : states })

#y = pandas.DataFrame({ 'state' : [ 'CA', 'OR' ], 'value' : [ 1, 2]})
y = pd.DataFrame({ 'states' : [ "CA", "NY" ], 'stateid' : [ 1, 2]})
z = pd.merge(x, y, how='left', on='states',)


x=
    dates   states
0   20140101    CA
1   20140102    NY
2   20140103    CA

y=
        stateid     states
0   1   CA
1   2   NY

z=   dates  states  stateid
0   20140101    CA  1
1   20140103    CA  1
2   20140102    NY  2

Note z is always sorted by "states" column whether argument sort=True or False.
This only happens when the x's states column is not unique. If x.states is unique(such as NY, CA, CT), sort=True and False behaves as expected.

This causes inconvenience when x is a time series and merge does not
preserve the time sequence.

@jreback
Copy link
Contributor

jreback commented Jun 4, 2014

hmm, could be a subtle bug in there; this didn't work in prior versions, but simply may be a function of the impl. if you have time and want to wade thru code, welcome to have a look!

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
Development

Successfully merging a pull request may close this issue.

2 participants