Skip to content

Commit

Permalink
Avoid unnecessary Index cast in IndexedFrame.index setter (#15843)
Browse files Browse the repository at this point in the history
Triaging recent dask-cuda [breakage](https://github.com/rapidsai/dask-cuda/actions/runs/9202583065/attempts/1) led me to #15781, where it seems like the passing of an index object directly to the `IndexedFrame.index` setter (and therefore, wrapping of this index in an `Index()` constructor) has caused proxifying issues on dask-cuda's end.

cc @rjzamora @mroeschke

Authors:
  - Charles Blackmon-Luca (https://github.com/charlesbluca)
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - Matthew Roeschke (https://github.com/mroeschke)

URL: #15843
  • Loading branch information
charlesbluca authored May 24, 2024
1 parent 68116d4 commit 78a0314
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,11 @@ def index(self, value):
f"Length mismatch: Expected axis has {old_length} elements, "
f"new values have {len(value)} elements"
)
self._index = Index(value)
# avoid unnecessary cast to Index
if not isinstance(value, BaseIndex):
value = Index(value)

self._index = value

@_cudf_nvtx_annotate
def replace(
Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3266,3 +3266,17 @@ def test_index_datetime_repeat():
actual = gidx.to_frame().repeat(5)

assert_eq(actual.index, expected)


@pytest.mark.parametrize(
"index",
[
cudf.Index([1]),
cudf.RangeIndex(1),
cudf.MultiIndex(levels=[[0]], codes=[[0]]),
],
)
def test_index_assignment_no_shallow_copy(index):
df = cudf.DataFrame(range(1))
df.index = index
assert df.index is index

0 comments on commit 78a0314

Please sign in to comment.