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 loc-getitem ordering when index contains duplicate labels #13659

Merged
merged 5 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,14 @@ def _getitem_tuple_arg(self, arg):
{tmp_col_name: column.arange(len(tmp_arg[0]))},
index=as_index(tmp_arg[0]),
)
cantor_name = "_" + "_".join(columns_df._data.names)
wence- marked this conversation as resolved.
Show resolved Hide resolved
columns_df[cantor_name] = column.arange(len(columns_df))
df = other_df.join(columns_df, how="inner")
# as join is not assigning any names to index,
# update it over here
df.index.name = columns_df.index.name
df = df.sort_values(tmp_col_name)
df.drop(columns=[tmp_col_name], inplace=True)
df = df.sort_values(by=[tmp_col_name, cantor_name])
df.drop(columns=[tmp_col_name, cantor_name], inplace=True)
# There were no indices found
if len(df) == 0:
raise KeyError(arg)
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _indices_from_labels(obj, labels):
rhs = cudf.DataFrame(
{"_": cudf.core.column.arange(len(obj))}, index=obj.index
)
return lhs.join(rhs).sort_values("__")["_"]
return lhs.join(rhs).sort_values(by=["__", "_"])["_"]


def _get_label_range_or_mask(index, start, stop, step):
Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,20 @@ def test_loc_missing_label_keyerror_issue_13379(index):
cdf.loc[[0, 5]]


@pytest.mark.parametrize("series", [True, False], ids=["Series", "DataFrame"])
def test_loc_repeated_label_ordering_issue_13658(series):
# https://github.com/rapidsai/cudf/issues/13658
values = range(2048)
index = [1 for _ in values]
if series:
frame = cudf.Series(values, index=index)
else:
frame = cudf.DataFrame({"a": values}, index=index)
expect = frame.to_pandas().loc[[1]]
actual = frame.loc[[1]]
assert_eq(actual, expect)


class TestLocIndexWithOrder:
# https://github.com/rapidsai/cudf/issues/12833
@pytest.fixture(params=["increasing", "decreasing", "neither"])
Expand Down