Skip to content

PERF: join empty frame #46015

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 6 commits into from
Feb 22, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Performance improvements
- Performance improvement in :meth:`.GroupBy.transform` when broadcasting values for user-defined functions (:issue:`45708`)
- Performance improvement in :meth:`.GroupBy.transform` for user-defined functions when only a single group exists (:issue:`44977`)
- Performance improvement in :func:`merge` when left and/or right are empty (:issue:`45838`)
- Performance improvement in :meth:`DataFrame.join` when left and/or right are empty (:issue:`46015`)
- Performance improvement in :class:`DataFrame` and :class:`Series` constructors for extension dtype scalars (:issue:`45854`)
-

Expand Down
14 changes: 12 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4544,12 +4544,22 @@ def join(

if len(other) == 0 and how in ("left", "outer"):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you make a if len(other) and if len(self) clause then sub if's here for the cases

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

join_index = self._view()
rindexer = np.repeat(np.intp(-1), len(join_index))
rindexer = np.broadcast_to(np.intp(-1), len(join_index))
return join_index, None, rindexer

if len(self) == 0 and how in ("right", "outer"):
join_index = other._view()
lindexer = np.repeat(np.intp(-1), len(join_index))
lindexer = np.broadcast_to(np.intp(-1), len(join_index))
return join_index, lindexer, None

if len(self) == 0 and how in ("left", "inner", "cross"):
join_index = self._view()
rindexer = np.array([])
return join_index, None, rindexer

if len(other) == 0 and how in ("right", "inner", "cross"):
join_index = other._view()
lindexer = np.array([])
return join_index, lindexer, None

if self._join_precedence < other._join_precedence:
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,44 @@ def test_join_multiindex_not_alphabetical_categorical(categories, values):
}
).set_index(["first", "second"])
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"left_empty, how, exp",
[
(False, "left", "left"),
(False, "right", "empty"),
(False, "inner", "empty"),
(False, "outer", "left"),
(False, "cross", "empty"),
(True, "left", "empty"),
(True, "right", "right"),
(True, "inner", "empty"),
(True, "outer", "right"),
(True, "cross", "empty"),
],
)
def test_join_empty(left_empty, how, exp):

left = DataFrame({"A": [2, 1], "B": [3, 4]}, dtype="int64").set_index("A")
right = DataFrame({"A": [1], "C": [5]}, dtype="int64").set_index("A")

if left_empty:
left = left.head(0)
else:
right = right.head(0)

result = left.join(right, how=how)

if exp == "left":
expected = DataFrame({"A": [2, 1], "B": [3, 4], "C": [np.nan, np.nan]})
expected = expected.set_index("A")
elif exp == "right":
expected = DataFrame({"B": [np.nan], "A": [1], "C": [5]})
expected = expected.set_index("A")
elif exp == "empty":
expected = DataFrame(index=Index([]), columns=["B", "C"], dtype="int64")
if how != "cross":
expected = expected.rename_axis("A")

tm.assert_frame_equal(result, expected)