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 an issue with loc when column names is MultiIndex #13929

Merged
merged 7 commits into from
Aug 22, 2023
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
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/column_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def _select_by_label_list_like(self, key: Any) -> ColumnAccessor:
def _select_by_label_grouped(self, key: Any) -> ColumnAccessor:
result = self._grouped_data[key]
if isinstance(result, cudf.core.column.ColumnBase):
return self.__class__({key: result})
return self.__class__({key: result}, multiindex=self.multiindex)
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
else:
if self.multiindex:
result = _to_flat_dict(result)
Expand Down
24 changes: 17 additions & 7 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ def _can_downcast_to_series(self, df, arg):
all_numeric = all(is_numeric_dtype(t) for t in dtypes)
if all_numeric:
return True
if isinstance(arg[1], tuple):
return True
if ncols == 1:
if type(arg[1]) is slice:
return False
if isinstance(arg[1], tuple):
# Multiindex indexing with a slice
if any(isinstance(v, slice) for v in arg):
return False
return len(arg[1]) == df._data.nlevels
if not (is_list_like(arg[1]) or is_column_like(arg[1])):
return True
return False
Expand All @@ -193,7 +193,10 @@ def _downcast_to_series(self, df, arg):
nrows, ncols = df.shape
# determine the axis along which the Series is taken:
if nrows == 1 and ncols == 1:
if is_scalar(arg[0]) and is_scalar(arg[1]):
if is_scalar(arg[0]) and (
is_scalar(arg[1])
or (df._data.multiindex and arg[1] in df._column_names)
):
return df[df._column_names[0]].iloc[0]
elif not is_scalar(arg[0]):
axis = 1
Expand Down Expand Up @@ -288,13 +291,20 @@ def _getitem_tuple_arg(self, arg):
)
else:
tmp_col_name = str(uuid4())
cantor_name = "_" + "_".join(
map(str, columns_df._data.names)
)
if columns_df._data.multiindex:
# column names must be appropriate length tuples
extra = tuple(
"" for _ in range(columns_df._data.nlevels - 1)
)
tmp_col_name = (tmp_col_name, *extra)
cantor_name = (cantor_name, *extra)
other_df = DataFrame(
{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,
Expand Down
25 changes: 25 additions & 0 deletions python/cudf/cudf/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,31 @@ def test_loc_index_notinindex_slice(
assert_eq(expect, actual)


@pytest.mark.parametrize(
"arg",
[
(2, ("one", "second")),
(slice(None, None, None), ("two", "first")),
(1, ("one", "first")),
(slice(None, None, None), ("two", "second")),
(slice(None, None, None), ("two", "first", "three")),
(3, ("two", "first", "three")),
(slice(None, None, None), ("two",)),
(0, ("two",)),
],
)
def test_loc_dataframe_column_multiindex(arg):
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
gdf = cudf.DataFrame(
[list("abcd"), list("efgh"), list("ijkl"), list("mnop")],
columns=cudf.MultiIndex.from_product(
[["one", "two"], ["first", "second"], ["three"]]
),
)
pdf = gdf.to_pandas()

assert_eq(gdf.loc[arg], pdf.loc[arg])


@pytest.mark.parametrize(
"arg", [slice(2, 4), slice(2, 5), slice(2.3, 5), slice(4.6, 6)]
)
Expand Down