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] Deprecate inplace parameters in categorical methods #12824

Merged
merged 4 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
78 changes: 77 additions & 1 deletion python/cudf/cudf/core/column/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ def as_ordered(self, inplace: bool = False) -> Optional[SeriesOrIndex]:
or return a copy of this categorical with
added categories.

.. deprecated:: 23.02
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see 23.02 here but 23.04 below, is this as intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this is intentional. inplace here was deprecated in 23.02. This is just updating the docstring.


The `inplace` parameter is is deprecated and
will be removed in a future version of cudf.
Setting categories as ordered will always
return a new Categorical object.

Returns
-------
Categorical
Expand Down Expand Up @@ -204,6 +211,13 @@ def as_unordered(self, inplace: bool = False) -> Optional[SeriesOrIndex]:
in-place or return a copy of this
categorical with ordered set to False.

.. deprecated:: 23.02

The `inplace` parameter is is deprecated and
will be removed in a future version of cudf.
Setting categories as unordered will always
return a new Categorical object.

Returns
-------
Categorical
Expand Down Expand Up @@ -286,6 +300,13 @@ def add_categories(
or return a copy of this categorical with
added categories.

.. deprecated:: 23.04

The `inplace` parameter is is deprecated and
will be removed in a future version of cudf.
Adding categories will always return a
new Categorical object.

Returns
-------
cat
Expand Down Expand Up @@ -318,7 +339,14 @@ def add_categories(
dtype: category
Categories (5, int64): [1, 2, 0, 3, 4]
"""

if inplace:
warnings.warn(
"The `inplace` parameter in cudf.Series.cat.add_categories "
"is deprecated and will be removed in a future version of "
"cudf. Adding categories will always return a new "
"Categorical object.",
FutureWarning,
)
old_categories = self._column.categories
new_categories = column.as_column(
new_categories,
Expand Down Expand Up @@ -371,6 +399,13 @@ def remove_categories(
inplace or return a copy of this categorical
with removed categories.

.. deprecated:: 23.04

The `inplace` parameter is is deprecated and
will be removed in a future version of cudf.
Removing categories will always return a
new Categorical object.

Returns
-------
cat
Expand Down Expand Up @@ -423,6 +458,16 @@ def remove_categories(
dtype: category
Categories (2, int64): [1, 2]
"""
if inplace:
warnings.warn(
"The `inplace` parameter in "
"cudf.Series.cat.remove_categories is deprecated and "
"will be removed in a future version of cudf. "
"Removing categories will always return a new "
"Categorical object.",
FutureWarning,
)

cats = self.categories.to_series()
removals = cudf.Series(removals, dtype=cats.dtype)
removals_mask = removals.isin(cats)
Expand Down Expand Up @@ -485,6 +530,13 @@ def set_categories(
or return a copy of this categorical with
reordered categories.

.. deprecated:: 23.04

The `inplace` parameter is is deprecated and
will be removed in a future version of cudf.
Setting categories will always return a
new Categorical object.

Returns
-------
cat
Expand Down Expand Up @@ -524,6 +576,14 @@ def set_categories(
dtype: category
Categories (2, int64): [1, 10]
"""
if inplace:
warnings.warn(
"The `inplace` parameter in cudf.Series.cat.set_categories is "
"deprecated and will be removed in a future version of cudf. "
"Setting categories will always return a new Categorical "
"object.",
FutureWarning,
)
return self._return_or_inplace(
self._column.set_categories(
new_categories=new_categories, ordered=ordered, rename=rename
Expand Down Expand Up @@ -556,6 +616,13 @@ def reorder_categories(
inplace or return a copy of this categorical
with reordered categories.

.. deprecated:: 23.04

The `inplace` parameter is is deprecated and
will be removed in a future version of cudf.
Reordering categories will always return a
new Categorical object.

Returns
-------
cat
Expand Down Expand Up @@ -597,6 +664,15 @@ def reorder_categories(
ValueError: items in new_categories are not the same as in
old categories
"""
if inplace:
warnings.warn(
"The `inplace` parameter in "
"cudf.Series.cat.reorder_categories is deprecated "
"and will be removed in a future version of cudf. "
"Reordering categories will always return a new "
"Categorical object.",
FutureWarning,
)
return self._return_or_inplace(
self._column.reorder_categories(new_categories, ordered=ordered),
inplace=inplace,
Expand Down
19 changes: 15 additions & 4 deletions python/cudf/cudf/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,13 @@ def test_categorical_reorder_categories(
"reorder_categories"
):
pd_sr_1 = pd_sr.cat.reorder_categories(list("cba"), **kwargs)
cd_sr_1 = cd_sr.cat.reorder_categories(list("cba"), **kwargs)
if inplace:
with pytest.warns(FutureWarning):
cd_sr_1 = cd_sr.cat.reorder_categories(list("cba"), **kwargs)
pd_sr_1 = pd_sr
cd_sr_1 = cd_sr
else:
cd_sr_1 = cd_sr.cat.reorder_categories(list("cba"), **kwargs)

assert_eq(pd_sr_1, cd_sr_1)

Expand Down Expand Up @@ -479,10 +482,14 @@ def test_categorical_add_categories(pd_str_cat, inplace):
"add_categories"
):
pd_sr_1 = pd_sr.cat.add_categories(["d"], inplace=inplace)
cd_sr_1 = cd_sr.cat.add_categories(["d"], inplace=inplace)

if inplace:
with pytest.warns(FutureWarning):
cd_sr_1 = cd_sr.cat.add_categories(["d"], inplace=inplace)
pd_sr_1 = pd_sr
cd_sr_1 = cd_sr
else:
cd_sr_1 = cd_sr.cat.add_categories(["d"], inplace=inplace)

assert "d" in pd_sr_1.cat.categories.to_list()
assert "d" in cd_sr_1.cat.categories.to_pandas().to_list()
Expand Down Expand Up @@ -516,10 +523,14 @@ def test_categorical_remove_categories(pd_str_cat, inplace):
"remove_categories"
):
pd_sr_1 = pd_sr.cat.remove_categories(["a"], inplace=inplace)
cd_sr_1 = cd_sr.cat.remove_categories(["a"], inplace=inplace)

if inplace:
with pytest.warns(FutureWarning):
cd_sr_1 = cd_sr.cat.remove_categories(["a"], inplace=inplace)
pd_sr_1 = pd_sr
cd_sr_1 = cd_sr
else:
cd_sr_1 = cd_sr.cat.remove_categories(["a"], inplace=inplace)

assert "a" not in pd_sr_1.cat.categories.to_list()
assert "a" not in cd_sr_1.cat.categories.to_pandas().to_list()
Expand All @@ -529,7 +540,7 @@ def test_categorical_remove_categories(pd_str_cat, inplace):
# test using ordered operators
with _hide_deprecated_pandas_categorical_inplace_warnings(
"remove_categories"
):
) as _, pytest.warns(FutureWarning) as _:
assert_exceptions_equal(
lfunc=cd_sr.to_pandas().cat.remove_categories,
rfunc=cd_sr.cat.remove_categories,
Expand Down