Skip to content

Fix left join turning into outer join #19624

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 4 commits into from
Feb 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ Reshaping
- Bug in timezone comparisons, manifesting as a conversion of the index to UTC in ``.concat()`` (:issue:`18523`)
- Bug in :func:`concat` when concatting sparse and dense series it returns only a ``SparseDataFrame``. Should be a ``DataFrame``. (:issue:`18914`, :issue:`18686`, and :issue:`16874`)
- Improved error message for :func:`DataFrame.merge` when there is no common merge key (:issue:`19427`)
-
- Bug in :func:`DataFrame.join` which does an *outer* instead of a *left* join when being called with multiple DataFrames and some have non-unique indices (:issue:`19624`)

Other
^^^^^
Expand Down
13 changes: 6 additions & 7 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5328,18 +5328,17 @@ def _join_compat(self, other, on=None, how='left', lsuffix='', rsuffix='',
raise ValueError('Joining multiple DataFrames only supported'
' for joining on index')

# join indexes only using concat
if how == 'left':
how = 'outer'
join_axes = [self.index]
else:
join_axes = None

frames = [self] + list(other)

can_concat = all(df.index.is_unique for df in frames)

# join indexes only using concat
if can_concat:
if how == 'left':
how = 'outer'
join_axes = [self.index]
else:
join_axes = None
return concat(frames, axis=1, join=how, join_axes=join_axes,
verify_integrity=True)

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/frame/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,20 @@ def test_join_period_index(frame_with_period_index):
index=frame_with_period_index.index)

tm.assert_frame_equal(joined, expected)


def test_join_left_sequence_non_unique_index():
# https://github.com/pandas-dev/pandas/issues/19607
df1 = DataFrame({'a': [0, 10, 20]}, index=[1, 2, 3])
df2 = DataFrame({'b': [100, 200, 300]}, index=[4, 3, 2])
df3 = DataFrame({'c': [400, 500, 600]}, index=[2, 2, 4])

joined = df1.join([df2, df3], how='left')

expected = DataFrame({
'a': [0, 10, 10, 20],
'b': [np.nan, 300, 300, 200],
'c': [np.nan, 400, 500, np.nan]
}, index=[1, 2, 2, 3])

tm.assert_frame_equal(joined, expected)