diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index d1c807279da..fc095b29ed5 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -291,12 +291,16 @@ def _getitem_tuple_arg(self, arg): {tmp_col_name: column.arange(len(tmp_arg[0]))}, index=as_index(tmp_arg[0]), ) + cantor_name = "_" + "_".join( + map(str, columns_df._data.names) + ) + 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) diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index 2d07fa23adb..68f1befc99c 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -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): diff --git a/python/cudf/cudf/tests/test_indexing.py b/python/cudf/cudf/tests/test_indexing.py index bf280ed7844..6aa5594d301 100644 --- a/python/cudf/cudf/tests/test_indexing.py +++ b/python/cudf/cudf/tests/test_indexing.py @@ -1994,6 +1994,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"])