Skip to content

Commit

Permalink
Ensure cudf.Series(cudf.Series(...)) creates a reference to the same …
Browse files Browse the repository at this point in the history
…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: #15845
  • Loading branch information
mroeschke authored May 24, 2024
1 parent a693e5b commit 72aa271
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
6 changes: 6 additions & 0 deletions python/cudf/cudf/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 72aa271

Please sign in to comment.