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

[REVIEW] Add no_default and adapt Series.reset_index to differentiate None for name parameter #13152

Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions python/cudf/cudf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
UInt64Index,
interval_range,
)
from cudf.core.default import no_default
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
from cudf.core.missing import NA
from cudf.core.multiindex import MultiIndex
from cudf.core.reshape import (
Expand Down
6 changes: 6 additions & 0 deletions python/cudf/cudf/core/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) 2023, NVIDIA CORPORATION.


from pandas.api.extensions import no_default

__all__ = ["no_default"]
7 changes: 5 additions & 2 deletions python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
to_cudf_compatible_scalar,
)
from cudf.utils.utils import _cudf_nvtx_annotate
from cudf.core.default import no_default


def _format_percentile_names(percentiles):
Expand Down Expand Up @@ -996,15 +997,17 @@ def reindex(self, *args, **kwargs):
""",
)
)
def reset_index(self, level=None, drop=False, name=None, inplace=False):
def reset_index(
self, level=None, drop=False, name=no_default, inplace=False
):
if not drop and inplace:
raise TypeError(
"Cannot reset_index inplace on a Series "
"to create a DataFrame"
)
data, index = self._reset_index(level=level, drop=drop)
if not drop:
if name is None:
if name is no_default:
name = 0 if self.name is None else self.name
data[name] = data.pop(self.name)
return cudf.core.dataframe.DataFrame._from_data(data, index)
Expand Down
16 changes: 5 additions & 11 deletions python/cudf/cudf/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ def test_nullable_bool_dtype_series(data, bool_dtype):
@pytest.mark.parametrize("level", [None, 0, "l0", 1, ["l0", 1]])
@pytest.mark.parametrize("drop", [True, False])
@pytest.mark.parametrize("original_name", [None, "original_ser"])
@pytest.mark.parametrize("name", [None, "ser"])
@pytest.mark.parametrize("name", [None, "ser", cudf.no_default])
@pytest.mark.parametrize("inplace", [True, False])
def test_reset_index(level, drop, inplace, original_name, name):
midx = pd.MultiIndex.from_tuples(
Expand All @@ -1422,10 +1422,8 @@ def test_reset_index(level, drop, inplace, original_name, name):
"test_reset_index_dup_level_name_exceptions"
)

with expect_warning_if(name is None and not drop):
expect = ps.reset_index(
level=level, drop=drop, name=name, inplace=inplace
)
expect = ps.reset_index(level=level, drop=drop, name=name, inplace=inplace)

got = gs.reset_index(level=level, drop=drop, name=name, inplace=inplace)
if inplace:
expect = ps
Expand All @@ -1450,10 +1448,7 @@ def test_reset_index_dup_level_name(level, drop, inplace, original_name, name):
"test_reset_index_dup_level_name_exceptions"
)

with expect_warning_if(name is None and not drop):
expect = ps.reset_index(
level=level, drop=drop, inplace=inplace, name=name
)
expect = ps.reset_index(level=level, drop=drop, inplace=inplace, name=name)
got = gs.reset_index(level=level, drop=drop, inplace=inplace, name=name)
if inplace:
expect = ps
Expand All @@ -1479,8 +1474,7 @@ def test_reset_index_named(drop, inplace, original_name, name):
"test_reset_index_dup_level_name_exceptions"
)

with expect_warning_if(name is None and not drop):
expect = ps.reset_index(drop=drop, inplace=inplace, name=name)
expect = ps.reset_index(drop=drop, inplace=inplace, name=name)
got = gs.reset_index(drop=drop, inplace=inplace, name=name)

if inplace:
Expand Down