Skip to content

BUG: preserve left frame order in left merge #8948

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

Merged
merged 1 commit into from
Dec 1, 2014

Conversation

behzadnouri
Copy link
Contributor

closes #7331

on master:

>>> left
      dates states
0  20140101     CA
1  20140102     NY
2  20140103     CA
>>> right
   stateid states
0        1     CA
1        2     NY
>>> pd.merge(left, right, how='left', on='states', sort=False)
      dates states  stateid
0  20140101     CA        1
1  20140103     CA        1
2  20140102     NY        2

DataFrame.join already works fine:

>>> left.join(right.set_index('states'), on='states', how='left')
      dates states  stateid
0  20140101     CA        1
1  20140102     NY        2
2  20140103     CA        1

on branch:

>>> pd.merge(left, right, how='left', on='states', sort=False)
      dates states  stateid
0  20140101     CA        1
1  20140102     NY        2
2  20140103     CA        1

>>> pd.merge(left, right, how='left', on='states', sort=True)
      dates states  stateid
0  20140101     CA        1
1  20140103     CA        1
2  20140102     NY        2

@jreback jreback added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Dec 1, 2014
@@ -479,8 +479,10 @@ def _get_join_indexers(left_keys, right_keys, sort=False, how='inner'):
left_group_key, right_group_key, max_groups = \
_factorize_keys(left_group_key, right_group_key, sort=sort)

# preserve left frame order if how == 'left' and sort == False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the only case where it needs/should be preserved? (e.g. what about other how's)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in pandas/src/join.pyx only left_outer_join accepts this extra argument sort, which was added in this commit to preserve left frame order.

I think it makes sense, because except for the case of left join preserving order is not well defined or I do not see an immediate use for it.

@jreback jreback added this to the 0.15.2 milestone Dec 1, 2014
jreback added a commit that referenced this pull request Dec 1, 2014
BUG: preserve left frame order in left merge
@jreback jreback merged commit e463818 into pandas-dev:master Dec 1, 2014
@jreback
Copy link
Contributor

jreback commented Dec 1, 2014

ok then!

btw, if you are looking for something to do......

#4088 ( I did this a while back, #6647 but it doesn't quite fix this)

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 this pull request may close these issues.

merge does not preserve order of data frame when left key has duplicate values
2 participants