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

Maintain the index name after .loc #11677

Merged
merged 1 commit into from
Sep 10, 2022
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
6 changes: 4 additions & 2 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,17 @@ def _getitem_tuple_arg(self, arg):
start = arg[0].start
if start is None:
start = self._frame.index[0]
df.index = as_index(start)
df.index = as_index(start, name=self._frame.index.name)
else:
row_selection = as_column(arg[0])
if is_bool_dtype(row_selection.dtype):
df.index = self._frame.index._apply_boolean_mask(
row_selection
)
else:
df.index = as_index(row_selection)
df.index = as_index(
row_selection, name=self._frame.index.name
)
# Step 4: Downcast
if self._can_downcast_to_series(df, arg):
return self._downcast_to_series(df, arg)
Expand Down
4 changes: 4 additions & 0 deletions python/cudf/cudf/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def test_dataframe_loc(scalar, step):
"d": np.random.random(size).astype(np.float64),
}
)
pdf.index.name = "index"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

.iloc behaved correctly before the changes in this PR. Just adding this here for completeness.


df = cudf.DataFrame.from_pandas(pdf)

Expand Down Expand Up @@ -629,6 +630,9 @@ def test_dataframe_iloc(nelem):
pdf["a"] = ha
pdf["b"] = hb

gdf.index.name = "index"
pdf.index.name = "index"

assert_eq(gdf.iloc[-1:1], pdf.iloc[-1:1])
assert_eq(gdf.iloc[nelem - 1 : -1], pdf.iloc[nelem - 1 : -1])
assert_eq(gdf.iloc[0 : nelem - 1], pdf.iloc[0 : nelem - 1])
Expand Down