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

Fix left join turning into outer join #19624

Merged
merged 4 commits into from Feb 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions pandas/core/frame.py
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
13 changes: 13 additions & 0 deletions pandas/tests/frame/test_join.py
Expand Up @@ -31,6 +31,11 @@ def right():
return DataFrame({'b': [300, 100, 200]}, index=[3, 1, 2])


@pytest.fixture
def right_non_unique():
Copy link
Contributor

Choose a reason for hiding this comment

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

Why'd you make this a fixture? If it's only used in one place then I'd prefer it be defined where it's used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, the below test is just the bare minimum, I think test_join needs to cover many more situations. But I will pull it into the method for now.

return DataFrame({'c': [400, 500, 600]}, index=[2, 2, 4])


@pytest.mark.parametrize(
"how, sort, expected",
[('inner', False, DataFrame({'a': [20, 10],
Expand Down Expand Up @@ -165,3 +170,11 @@ 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(left, right, right_non_unique):
# left join sequence of dataframes with non-unique indices (issue #19607)
joined = left.join([right_non_unique], how='left')
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also build the expected output and tm.assert_frame_equal against that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That should definitely be possible

tm.assert_index_equal(
joined.index.unique().sort_values(),
left.index.sort_values())