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 joining on indexes with duplicate level names #9137

Merged
merged 2 commits into from
Aug 28, 2021
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
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/join/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def _compute_join_keys(self):
left_keys.extend(
[
_Indexer(name=on, index=True)
for on in self.lhs.index.names
for on in self.lhs.index._data.names
]
)
if self.left_on:
Expand All @@ -223,7 +223,7 @@ def _compute_join_keys(self):
right_keys.extend(
[
_Indexer(name=on, index=True)
for on in self.rhs.index.names
for on in self.rhs.index._data.names
]
)
if self.right_on:
Expand Down
10 changes: 9 additions & 1 deletion python/cudf/cudf/core/multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,9 +1524,17 @@ def from_pandas(cls, multiindex, nan_as_null=None):
if not isinstance(multiindex, pd.MultiIndex):
raise TypeError("not a pandas.MultiIndex")

# if `multiindex` has two or more levels that
# have the same name, then `multiindex.to_frame()`
# results in a DataFrame containing only one of those
# levels. Thus, set `names` to some tuple of unique values
# and then call `multiindex.to_frame(name=names)`,
# which preserves all levels of `multiindex`.
Comment on lines +1527 to +1532
Copy link
Member

Choose a reason for hiding this comment

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

This is an excellent comment, as it's quite unclear from the following line of code why it's necessary

names = tuple(range(len(multiindex.names)))

mi = cls(
names=multiindex.names,
source_data=multiindex.to_frame(),
source_data=multiindex.to_frame(name=names),
nan_as_null=nan_as_null,
)

Expand Down
20 changes: 20 additions & 0 deletions python/cudf/cudf/tests/test_joining.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,3 +2112,23 @@ def test_string_join_values_nulls():
got = got.sort_values(by=["a", "b", "c"]).reset_index(drop=True)

assert_join_results_equal(expect, got, how="left")


def test_join_on_index_with_duplicate_names():
# although index levels with duplicate names are poorly supported
# overall, we *should* be able to join on them:
lhs = pd.DataFrame({"a": [1, 2, 3]})
rhs = pd.DataFrame({"b": [1, 2, 3]})
lhs.index = pd.MultiIndex.from_tuples(
[(1, 1), (1, 2), (2, 1)], names=["x", "x"]
)
rhs.index = pd.MultiIndex.from_tuples(
[(1, 1), (1, 3), (2, 1)], names=["x", "x"]
)
expect = lhs.join(rhs, how="inner")

lhs = cudf.from_pandas(lhs)
rhs = cudf.from_pandas(rhs)
got = lhs.join(rhs, how="inner")

assert_join_results_equal(expect, got, how="inner")