From 72aa271a6ad8cfdcd4373ceadd777b4800fd26c4 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 24 May 2024 06:24:37 -1000 Subject: [PATCH] Ensure cudf.Series(cudf.Series(...)) creates a reference to the same index (#15845) Aligns these behaviors ```python In [1]: import pandas as pd In [3]: ser1 = pd.Series(range(3), index=list("Abc")) In [4]: ser2 = pd.Series(ser1) In [5]: ser1.index is ser2.index Out[5]: True In [6]: import cudf In [7]: ser1 = cudf.Series(range(3), index=list("Abc")) In [8]: ser2 = cudf.Series(ser1) In [9]: ser1.index is ser2.index Out[9]: False ``` Authors: - Matthew Roeschke (https://github.com/mroeschke) Approvers: - Vyas Ramasubramani (https://github.com/vyasr) - GALI PREM SAGAR (https://github.com/galipremsagar) URL: https://github.com/rapidsai/cudf/pull/15845 --- python/cudf/cudf/core/series.py | 4 +++- python/cudf/cudf/tests/test_series.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/python/cudf/cudf/core/series.py b/python/cudf/cudf/core/series.py index 41fbf269699..908347e389b 100644 --- a/python/cudf/cudf/core/series.py +++ b/python/cudf/cudf/core/series.py @@ -595,8 +595,10 @@ def __init__( data = data.copy(deep=True) name_from_data = data.name column = as_column(data, nan_as_null=nan_as_null, dtype=dtype) - if isinstance(data, (pd.Series, Series)): + if isinstance(data, pd.Series): index_from_data = as_index(data.index) + elif isinstance(data, Series): + index_from_data = data.index elif isinstance(data, ColumnAccessor): raise TypeError( "Use cudf.Series._from_data for constructing a Series from " diff --git a/python/cudf/cudf/tests/test_series.py b/python/cudf/cudf/tests/test_series.py index 9aeae566730..323716d5fc3 100644 --- a/python/cudf/cudf/tests/test_series.py +++ b/python/cudf/cudf/tests/test_series.py @@ -2835,3 +2835,9 @@ def test_timedelta_series_init(data): actual = cudf.Series(scalar) assert_eq(expected, actual) + + +def test_series_from_series_index_no_shallow_copy(): + ser1 = cudf.Series(range(3), index=list("abc")) + ser2 = cudf.Series(ser1) + assert ser1.index is ser2.index