diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index 394904c5855..b4a689804c7 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -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( diff --git a/python/cudf/cudf/tests/test_index.py b/python/cudf/cudf/tests/test_index.py index 8e7532d044d..b92ae1b3364 100644 --- a/python/cudf/cudf/tests/test_index.py +++ b/python/cudf/cudf/tests/test_index.py @@ -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